NO_CONSTRAINT ineffective

hibernate.hbm2ddl.default_constraint_mode=NO_CONSTRAINT ineffective

Hi. I use version 6.4 and HSQLDB “mem:” database for my tests and I tried to disable foreign key DDLs because Hibernate can’t validate the schema FKs when weird quoted table names are used.

            new StandardServiceRegistryBuilder()
                // only affects insert/delete
                // .applySetting(AvailableSettings.STATEMENT_BATCH_SIZE, 10)
                .applySetting(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, true)
                .applySetting(
                    AvailableSettings.HBM2DDL_AUTO,
                    org.hibernate.tool.schema.Action.UPDATE.getExternalHbm2ddlName())
                .applySetting(
                    AvailableSettings.HBM2DDL_DEFAULT_CONSTRAINT_MODE,
                    jakarta.persistence.ConstraintMode.NO_CONSTRAINT.name())
                ...
                .build();

However, when I re-initialize the session factory on the same DB it fails with: Attempt to resolve foreign key metadata from JDBC metadata failed to find column mappings for foreign key named FK6opwmm8uu9bbwugb8kf4kl2g3. Looks like the "NO_CONSTRAINT" is ignored and the FK gets created.. Of course, if I use explicit simple uppercase table names the error goes away, but the FKs are still created.

// first init:
10:14:11.462 [main] DEBUG org.hibernate.SQL - create table "TestCache2$Language" ("id" bigint not null, "code" varchar(255), "name" varchar(255), primary key ("id"))
10:14:11.463 [main] DEBUG org.hibernate.SQL - create table "X.Book" ("submetaobjectname" varchar(31) not null, "id" bigint not null, "name" varchar(255), primary key ("id"))
10:14:11.464 [main] DEBUG org.hibernate.SQL - create table "X.Book1" ("b1" varchar(255), "id" bigint not null, primary key ("id"))
10:14:11.465 [main] DEBUG org.hibernate.SQL - create table "X.Book2" ("b2" varchar(255), "id" bigint not null, primary key ("id"))
10:14:11.465 [main] DEBUG org.hibernate.SQL - create table "b2l" ("bookid" bigint not null, "langid" bigint not null, primary key ("bookid", "langid"))
10:14:11.467 [main] DEBUG org.hibernate.SQL - alter table "X.Book1" add constraint "FK6opwmm8uu9bbwugb8kf4kl2g3" foreign key ("id") references "X.Book"
10:14:11.468 [main] DEBUG org.hibernate.SQL - alter table "X.Book2" add constraint "FK2rrple5fw9k4lem87pc3wl12k" foreign key ("id") references "X.Book"
...
// second init:
10:14:54.249 [main] INFO  org.hibernate.cache.internal.RegionFactoryInitiator - HHH000025: Second-level cache region factory [org.hibernate.cache.jcache.internal.JCacheRegionFactory]
10:14:54.425 [main] INFO  org.hibernate.tool.schema.extract.internal.AbstractInformationExtractorImpl - HHH000262: Table not found: X.Book
Exception in thread "main" org.hibernate.tool.schema.spi.SchemaManagementException: Attempt to resolve foreign key metadata from JDBC metadata failed to find column mappings for foreign key named [FK6opwmm8uu9bbwugb8kf4kl2g3]
	at org.hibernate.tool.schema.extract.internal.AbstractInformationExtractorImpl$ForeignKeyBuilderImpl.build(AbstractInformationExtractorImpl.java:1383)
	at org.hibernate.tool.schema.extract.internal.AbstractInformationExtractorImpl$ForeignKeyBuilderImpl.build(AbstractInformationExtractorImpl.java:1364)
	at org.hibernate.tool.schema.extract.internal.AbstractInformationExtractorImpl.getForeignKeys(AbstractInformationExtractorImpl.java:1348)
	at org.hibernate.tool.schema.extract.internal.TableInformationImpl.foreignKeys(TableInformationImpl.java:97)
	at org.hibernate.tool.schema.extract.internal.TableInformationImpl.getForeignKeys(TableInformationImpl.java:91)
	at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.equivalentForeignKeyExistsInDatabase(AbstractSchemaMigrator.java:483)
	at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.checkForExistingForeignKey(AbstractSchemaMigrator.java:475)
	at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applyForeignKeys(AbstractSchemaMigrator.java:442)
	at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:268)
	at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:117)
	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:286)
	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.lambda$process$5(SchemaManagementToolCoordinator.java:145)
	at java.base/java.util.HashMap.forEach(HashMap.java:1336)
	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:142)
	at org.hibernate.boot.internal.SessionFactoryObserverForSchemaExport.sessionFactoryCreated(SessionFactoryObserverForSchemaExport.java:37)
	at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35)
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:315)
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:450)
	at snippet.TestCache2.makeSessionFactory(TestCache2.java:446)
	at snippet.TestCache2.main(TestCache2.java:210)

Looks like a bug. Please try to create a reproducer with our test case template (https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our issue tracker(https://hibernate.atlassian.net) and attach that reproducer.

https://hibernate.atlassian.net/browse/HHH-17550

1 Like