Skip to content

Commit

Permalink
HHH-8390 generate FK after UK
Browse files Browse the repository at this point in the history
  • Loading branch information
brmeyer committed Aug 1, 2013
1 parent 31b881b commit 580a713
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java
Expand Up @@ -1092,9 +1092,17 @@ public String[] generateSchemaCreationScript(Dialect dialect) throws HibernateEx
)
);
}
}
}

// Foreign keys must be created *after* unique keys for numerous DBs. See HH-8390.
iter = getTableMappings();
while ( iter.hasNext() ) {
Table table = (Table) iter.next();
if ( table.isPhysicalTable() ) {

if ( dialect.hasAlterTable() ) {
subIter = table.getForeignKeyIterator();
Iterator subIter = table.getForeignKeyIterator();
while ( subIter.hasNext() ) {
ForeignKey fk = (ForeignKey) subIter.next();
if ( fk.isPhysicalConstraint() ) {
Expand Down Expand Up @@ -1232,6 +1240,33 @@ public List<SchemaUpdateScript> generateSchemaUpdateScriptList(Dialect dialect,
}
}

Iterator subIter = table.getIndexIterator();
while ( subIter.hasNext() ) {
final Index index = (Index) subIter.next();
// Skip if index already exists
if ( tableInfo != null && StringHelper.isNotEmpty( index.getName() ) ) {
final IndexMetadata meta = tableInfo.getIndexMetadata( index.getName() );
if ( meta != null ) {
continue;
}
}
scripts.add( new SchemaUpdateScript( index.sqlCreateString( dialect, mapping, tableCatalog,
tableSchema ), false ) );
}
}
}

// Foreign keys must be created *after* unique keys for numerous DBs. See HH-8390.
iter = getTableMappings();
while ( iter.hasNext() ) {
Table table = (Table) iter.next();
String tableSchema = ( table.getSchema() == null ) ? defaultSchema : table.getSchema();
String tableCatalog = ( table.getCatalog() == null ) ? defaultCatalog : table.getCatalog();
if ( table.isPhysicalTable() ) {

TableMetadata tableInfo = databaseMetadata.getTableMetadata( table.getName(), tableSchema,
tableCatalog, table.isQuoted() );

if ( dialect.hasAlterTable() ) {
Iterator subIter = table.getForeignKeyIterator();
while ( subIter.hasNext() ) {
Expand All @@ -1247,20 +1282,6 @@ public List<SchemaUpdateScript> generateSchemaUpdateScriptList(Dialect dialect,
}
}
}

Iterator subIter = table.getIndexIterator();
while ( subIter.hasNext() ) {
final Index index = (Index) subIter.next();
// Skip if index already exists
if ( tableInfo != null && StringHelper.isNotEmpty( index.getName() ) ) {
final IndexMetadata meta = tableInfo.getIndexMetadata( index.getName() );
if ( meta != null ) {
continue;
}
}
scripts.add( new SchemaUpdateScript( index.sqlCreateString( dialect, mapping, tableCatalog,
tableSchema ), false ) );
}
}
}

Expand Down

0 comments on commit 580a713

Please sign in to comment.