From 6ffe81f3e51125635a43213ba2fe19d823fb6b8e Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 17 Jul 2025 16:38:57 +0200 Subject: [PATCH] HHH-19614 Apply column check constraints as table constraints if possible --- .../internal/StandardTableExporter.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableExporter.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableExporter.java index 6fdd8ed162cd..98358f28aab0 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableExporter.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableExporter.java @@ -197,6 +197,41 @@ protected void applyTableTypeString(StringBuilder buf) { protected void applyTableCheck(Table table, StringBuilder buf) { if ( dialect.supportsTableCheck() ) { + if ( !dialect.supportsColumnCheck() ) { + for ( Column column : table.getColumns() ) { + // some databases (Maria, SQL Server) don't like multiple 'check' clauses + final List checkConstraints = column.getCheckConstraints(); + long anonConstraints = checkConstraints.stream().filter( CheckConstraint::isAnonymous ).count(); + if ( anonConstraints == 1 ) { + for ( CheckConstraint constraint : checkConstraints ) { + buf.append( "," ).append( constraint.constraintString( dialect ) ); + } + } + else { + boolean first = true; + for ( CheckConstraint constraint : checkConstraints ) { + if ( constraint.isAnonymous() ) { + if ( first ) { + buf.append( "," ).append( " check (" ); + first = false; + } + else { + buf.append( " and " ); + } + buf.append( constraint.getConstraintInParens() ); + } + } + if ( !first ) { + buf.append( ")" ); + } + for ( CheckConstraint constraint : checkConstraints ) { + if ( constraint.isNamed() ) { + buf.append( constraint.constraintString( dialect ) ); + } + } + } + } + } for ( CheckConstraint constraint : table.getChecks() ) { buf.append( "," ).append( constraint.constraintString( dialect ) ); }