SchemaExport creates foreign keys too early if across schema

Description

Our schema export used to work perfectly with Hibernate 4.x but fails after update to 5.0.

It turned out that the new SchemaMigratorImpl tries to create the foreign keys after the tables of a schema are complete. This however fails if those keys refer to tables in a different schema which have not yet been processed.

The old Configuration.generateSchemaUpdateScriptList created the foreign keys after the tables from all schema had been processed.

We created a test project on https://github.com/abenneke/sandbox/tree/master/hibernate-schema where

FirstParent from schema FIRST has children from SecondChild from schema SECOND
and
SecondParent from schema SECOND has children from FirstChild from schema FIRST

(both directions to make the creation order irrelevant, but note that there is no circle)

SchemaExport of Hibernate 4 created those tables this way:

create table FIRST.FirstChild (id varchar(255) not null, parent_id varchar(255), primary key (id)) create table FIRST.FirstParent (id varchar(255) not null, primary key (id)) create table SECOND.SecondChild (id varchar(255) not null, parent_id varchar(255), primary key (id)) create table SECOND.SecondParent (id varchar(255) not null, primary key (id)) alter table FIRST.FirstChild add constraint FK... foreign key (parent_id) references SECOND.SecondParent alter table SECOND.SecondChild add constraint FK... foreign key (parent_id) references FIRST.FirstParent

whereas Hibernate 5 now fails with

create table FIRST.FirstChild (id varchar(255) not null, parent_id varchar(255), primary key (id)) create table FIRST.FirstParent (id varchar(255) not null, primary key (id)) alter table FIRST.FirstChild add constraint FK... foreign key (parent_id) references SECOND.SecondParent HHH000389: Unsuccessful: alter table FIRST.FirstChild add constraint FK... foreign key (parent_id) references SECOND.SecondParent

Please note also that hibernate.hbm2ddl.auto with create simply reports this error and continues and update fails with a SchemaManagementException: Unable to execute schema management to JDBC target

Activity

Show:

Andreas Benneke January 8, 2016 at 8:10 PM

Perfect, thanks again!

Andrea Boriero January 7, 2016 at 2:28 PM

Hi ,

I have create anothe issue https://hibernate.atlassian.net/browse/HHH-10425 for the SchemaMigratorImpl.

Thanks a lot

Andreas Benneke January 7, 2016 at 1:52 PM

Thank you for fixing this so quick in SchemaCreatorImpl! slightly smiling face

Please note, that the problem exists in SchemaMigratorImpl as well.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created January 6, 2016 at 10:58 PM
Updated January 13, 2016 at 6:27 PM
Resolved January 7, 2016 at 11:09 AM

Flag notifications