Permalink
Browse files

HHH-7612 - Fix and test

  • Loading branch information...
1 parent 468c583 commit 14d1c626a5b3e678535aa23f689cadb0a0c34fd9 @lukasz-antoniak lukasz-antoniak committed Nov 24, 2012
@@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -457,7 +458,7 @@ public String sqlTemporaryTableCreateString(Dialect dialect, Mapping mapping) th
.append( ' ' )
.append( name )
.append( " (" );
- Iterator itr = getColumnIterator();
+ Iterator itr = getSortedColumnIterator();
while ( itr.hasNext() ) {
final Column column = (Column) itr.next();
buffer.append( column.getQuotedName( dialect ) ).append( ' ' );
@@ -491,7 +492,7 @@ public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog,
pkname = ( (Column) getPrimaryKey().getColumnIterator().next() ).getQuotedName( dialect );
}
- Iterator iter = getColumnIterator();
+ Iterator iter = getSortedColumnIterator();
while ( iter.hasNext() ) {
Column col = (Column) iter.next();
@@ -591,6 +592,33 @@ public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog,
return buf.append( dialect.getTableTypeString() ).toString();
}
+ /**
+ * @return Sorted column list so that primary key appears first, followed by foreign keys and other properties.
+ * Within each group columns are not sorted in any way.
+ */
+ private Iterator<Column> getSortedColumnIterator() {
+ final LinkedHashSet<Column> sortedColumns = new LinkedHashSet<Column>();
+ // Adding primary key columns.
+ if ( hasPrimaryKey() ) {
+ sortedColumns.addAll( getPrimaryKey().getColumns() );
+ }
+ // Adding foreign key columns.
+ Iterator iter = getForeignKeyIterator();
+ while ( iter.hasNext() ) {
+ ForeignKey fk = (ForeignKey) iter.next();
+ sortedColumns.addAll( fk.getColumns() );
+ }
+ // Adding other columns.
+ iter = getColumnIterator();
+ while ( iter.hasNext() ) {
+ final Column column = (Column) iter.next();
+ if ( ! sortedColumns.contains( column ) ) {
+ sortedColumns.add( column );
+ }
+ }
+ return sortedColumns.iterator();
+ }
+
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
StringBuilder buf = new StringBuilder( "drop table " );
if ( dialect.supportsIfExistsBeforeTableName() ) {
@@ -34,8 +34,8 @@ public void testGeneratedSql() {
for (String s : schema) {
log.debug(s);
}
- String expectedMappingTableSql = "create table personAddress (address_id numeric(19,0), " +
- "person_id numeric(19,0) not null, primary key (person_id))";
+ String expectedMappingTableSql = "create table personAddress (person_id numeric(19,0) not null, " +
+ "address_id numeric(19,0), primary key (person_id))";
Assert.assertEquals( "Wrong SQL", expectedMappingTableSql, schema[2] );
} catch (Exception e) {
Assert.fail(e.getMessage());
@@ -1,3 +1,3 @@
INSERT INTO `vgras007_v031` VALUES ('ZZZ','00',1);
-INSERT INTO `vgras029_v031` VALUES (1,'Foo Foo Foo',1), (1,'Bar Bar Bar',2);
+INSERT INTO `vgras029_v031` VALUES (1,1,'Foo Foo Foo'), (1,2,'Bar Bar Bar');
@@ -0,0 +1,36 @@
+package org.hibernate.test.schemaupdate;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.hibernate.dialect.H2Dialect;
+import org.hibernate.engine.spi.Mapping;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.test.onetomany.Node;
+import org.hibernate.testing.RequiresDialect;
+import org.hibernate.testing.TestForIssue;
+import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
+
+/**
+ * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
+ */
+public class SchemaGenerationTest extends BaseCoreFunctionalTestCase {
+ @Override
+ protected String[] getMappings() {
+ return new String[] { "onetomany/Node.hbm.xml" };
+ }
+
+ @Test
+ @TestForIssue( jiraKey = "HHH-7612" )
+ @RequiresDialect( H2Dialect.class )
+ public void testSqlCreatePrimaryAndForeignKeyOrder() {
+ final Mapping mappings = configuration().buildMapping();
+ final PersistentClass persistentClass = configuration().getClassMapping( Node.class.getName() );
+ final String sqlCreate = persistentClass.getTable().sqlCreateString( getDialect(), mappings, null, null );
+ Assert.assertEquals(
+ "PK and FK columns should appear first in CREATE TABLE statement.",
+ "create table Node (id integer not null, node_id integer, description varchar(255), idx integer, primary key (id))",
+ sqlCreate
+ );
+ }
+}

0 comments on commit 14d1c62

Please sign in to comment.