parent
6498ca969c
commit
d9e1a1c6e8
|
@ -29,6 +29,7 @@ import java.util.Collections;
|
|||
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 class Table implements RelationalModel, Serializable {
|
|||
.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 class Table implements RelationalModel, Serializable {
|
|||
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 class Table implements RelationalModel, Serializable {
|
|||
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 class NullablePrimaryKeyTest {
|
|||
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
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue