Skip to content

Commit 87e98b7

Browse files
committed
HHH-19614 Apply column check constraints as table constraints if possible
1 parent 3763a29 commit 87e98b7

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableExporter.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,41 @@ protected void applyTableTypeString(StringBuilder buf) {
197197

198198
protected void applyTableCheck(Table table, StringBuilder buf) {
199199
if ( dialect.supportsTableCheck() ) {
200+
if ( !dialect.supportsColumnCheck() ) {
201+
for ( Column column : table.getColumns() ) {
202+
// some databases (Maria, SQL Server) don't like multiple 'check' clauses
203+
final List<CheckConstraint> checkConstraints = column.getCheckConstraints();
204+
long anonConstraints = checkConstraints.stream().filter( CheckConstraint::isAnonymous ).count();
205+
if ( anonConstraints == 1 ) {
206+
for ( CheckConstraint constraint : checkConstraints ) {
207+
buf.append( "," ).append( constraint.constraintString( dialect ) );
208+
}
209+
}
210+
else {
211+
boolean first = true;
212+
for ( CheckConstraint constraint : checkConstraints ) {
213+
if ( constraint.isAnonymous() ) {
214+
if ( first ) {
215+
buf.append( "," ).append( " check (" );
216+
first = false;
217+
}
218+
else {
219+
buf.append( " and " );
220+
}
221+
buf.append( constraint.getConstraintInParens() );
222+
}
223+
}
224+
if ( !first ) {
225+
buf.append( ")" );
226+
}
227+
for ( CheckConstraint constraint : checkConstraints ) {
228+
if ( constraint.isNamed() ) {
229+
buf.append( constraint.constraintString( dialect ) );
230+
}
231+
}
232+
}
233+
}
234+
}
200235
for ( CheckConstraint constraint : table.getChecks() ) {
201236
buf.append( "," ).append( constraint.constraintString( dialect ) );
202237
}

0 commit comments

Comments
 (0)