From c7bafd646aba6806270766fa8ca221ab1d25aadc Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 25 Jul 2023 12:27:40 +0200 Subject: [PATCH] HHH-16895 @Check constraint not generated when annotated on entity --- .../internal/InFlightMetadataCollectorImpl.java | 7 +++++++ .../org/hibernate/mapping/PersistentClass.java | 17 ++++++++--------- .../main/java/org/hibernate/sql/Template.java | 1 - 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java index 8a2d92f255..6bd7033691 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java @@ -2354,6 +2354,12 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector, final Dialect dialect = getDatabase().getJdbcEnvironment().getDialect(); for ( PersistentClass entityBinding : entityBindingMap.values() ) { + entityBinding.assignCheckConstraintsToTable( + dialect, + bootstrapContext.getTypeConfiguration(), + bootstrapContext.getFunctionRegistry() + ); + if ( entityBinding.isInherited() ) { continue; } @@ -2363,6 +2369,7 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector, dialect, (RootClass) entityBinding ); + } for ( Collection collection : collectionBindingMap.values() ) { diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java index 9a7cbb60bf..fdc9616bc0 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java @@ -1258,23 +1258,22 @@ public abstract class PersistentClass implements IdentifiableTypeClass, Attribut this.superMappedSuperclass = superMappedSuperclass; } - // End of @MappedSuperclass support + public void assignCheckConstraintsToTable(Dialect dialect, TypeConfiguration types, SqmFunctionRegistry functions) { + for ( CheckConstraint checkConstraint : checkConstraints ) { + container( collectColumnNames( checkConstraint.getConstraint(), dialect, types, functions ) ) + .getTable().addCheck( checkConstraint ); + } + } + // End of @MappedSuperclass support public void prepareForMappingModel(RuntimeModelCreationContext context) { if ( !joins.isEmpty() ) { // we need to deal with references to secondary tables - // in SQL formulas and check constraints - + // in SQL formulas final Dialect dialect = context.getDialect(); final TypeConfiguration types = context.getTypeConfiguration(); final SqmFunctionRegistry functions = context.getFunctionRegistry(); - // assign check constraints to the right table - for ( CheckConstraint checkConstraint : checkConstraints ) { - container( collectColumnNames( checkConstraint.getConstraint(), dialect, types, functions ) ) - .getTable().addCheck( checkConstraint ); - } - // now, move @Formulas to the correct AttributeContainers //TODO: skip this step for hbm.xml for ( Property property : new ArrayList<>( properties ) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/sql/Template.java b/hibernate-core/src/main/java/org/hibernate/sql/Template.java index 2543390f87..3924b5189b 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/Template.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/Template.java @@ -15,7 +15,6 @@ import java.util.StringTokenizer; import org.hibernate.HibernateException; import org.hibernate.dialect.Dialect; -import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.query.sqm.function.SqmFunctionDescriptor; import org.hibernate.query.sqm.function.SqmFunctionRegistry; import org.hibernate.type.spi.TypeConfiguration;