From 064c355cd0ccde908413686370e5ca4da1d2b5d0 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Mon, 22 Oct 2012 17:06:14 +0800 Subject: [PATCH] HHH-7706 some minor issues in the metamodel branch --- .../hibernate/metamodel/internal/Binder.java | 45 +++++++++++-------- .../AbstractTableSpecification.java | 24 +++++----- .../test/annotations/EntityTest.java | 39 ++++------------ .../collection/bag/PersistentBagTest.java | 2 - .../collection/list/PersistentListTest.java | 2 - .../CollectionReattachmentTest.java | 2 - .../reattachment/ProxyReattachmentTest.java | 6 --- .../junit4/BaseCoreFunctionalTestCase.java | 8 +++- 8 files changed, 56 insertions(+), 72 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java index 8bc5284c93..badd71ea35 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java @@ -1424,13 +1424,20 @@ public class Binder { } private void bindPrimaryTable( final EntityBinding entityBinding, final EntitySource entitySource ) { - entityBinding.setPrimaryTable( createTable( entitySource.getPrimaryTable(), new DefaultNamingStrategy() { + final TableSpecification table = createTable( + entitySource.getPrimaryTable(), new DefaultNamingStrategy() { @Override public String defaultName() { - return bindingContexts.peek().getNamingStrategy().classToTableName( entityBinding.getEntity().getClassName() ); + String name = StringHelper.isNotEmpty( entityBinding.getJpaEntityName() ) ? entityBinding.getJpaEntityName() : entityBinding + .getEntity() + .getClassName(); + return bindingContexts.peek().getNamingStrategy().classToTableName( name ); } - } ) ); + } + ); + entityBinding.setPrimaryTable( table ); + entityBinding.setPrimaryTableName( table.getLogicalName().getText() ); } private void bindSecondaryTables( final EntityBinding entityBinding, final EntitySource entitySource ) { @@ -1587,13 +1594,14 @@ public class Binder { } private void bindUniqueConstraints( final EntityBinding entityBinding, final EntitySource entitySource ) { + int uniqueIndexPerTable = 0; for ( final ConstraintSource constraintSource : entitySource.getConstraints() ) { if ( UniqueConstraintSource.class.isInstance( constraintSource ) ) { final TableSpecification table = entityBinding.locateTable( constraintSource.getTableName() ); - final String constraintName = constraintSource.name(); - if ( constraintName == null ) { - throw new NotYetImplementedException( "create default constraint name" ); - } + uniqueIndexPerTable++; + final String constraintName = StringHelper.isEmpty( constraintSource.name() ) + ? "key" + uniqueIndexPerTable + : constraintSource.name(); final UniqueKey uniqueKey = table.getOrCreateUniqueKey( constraintName ); for ( final String columnName : constraintSource.columnNames() ) { uniqueKey.addColumn( table.locateOrCreateColumn( quotedIdentifier( columnName ) ) ); @@ -1808,6 +1816,18 @@ public class Binder { entityClassName, bindingContext.makeClassReference( entityClassName ), superEntityBinding == null ? null : superEntityBinding.getEntity() ) ); + entityBinding.setJpaEntityName( entitySource.getJpaEntityName() ); //must before creating primary table + entityBinding.setDynamicUpdate( entitySource.isDynamicUpdate() ); + entityBinding.setDynamicInsert( entitySource.isDynamicInsert() ); + entityBinding.setBatchSize( entitySource.getBatchSize() ); + entityBinding.setSelectBeforeUpdate( entitySource.isSelectBeforeUpdate() ); + entityBinding.setAbstract( entitySource.isAbstract() ); + + entityBinding.setCustomLoaderName( entitySource.getCustomLoaderName() ); + entityBinding.setCustomInsert( entitySource.getCustomSqlInsert() ); + entityBinding.setCustomUpdate( entitySource.getCustomSqlUpdate() ); + entityBinding.setCustomDelete( entitySource.getCustomSqlDelete() ); + entityBinding.setJpaCallbackClasses( entitySource.getJpaCallbackClasses() ); // Create relational table if ( superEntityBinding != null && inheritanceType == InheritanceType.SINGLE_TABLE ) { entityBinding.setPrimaryTable( superEntityBinding.getPrimaryTable() ); @@ -1851,18 +1871,7 @@ public class Binder { entitySource.getMetaAttributeSources(), true, metadata.getGlobalMetaAttributeContext() ) ); - entityBinding.setJpaEntityName( entitySource.getJpaEntityName() ); - entityBinding.setDynamicUpdate( entitySource.isDynamicUpdate() ); - entityBinding.setDynamicInsert( entitySource.isDynamicInsert() ); - entityBinding.setBatchSize( entitySource.getBatchSize() ); - entityBinding.setSelectBeforeUpdate( entitySource.isSelectBeforeUpdate() ); - entityBinding.setAbstract( entitySource.isAbstract() ); - entityBinding.setCustomLoaderName( entitySource.getCustomLoaderName() ); - entityBinding.setCustomInsert( entitySource.getCustomSqlInsert() ); - entityBinding.setCustomUpdate( entitySource.getCustomSqlUpdate() ); - entityBinding.setCustomDelete( entitySource.getCustomSqlDelete() ); - entityBinding.setJpaCallbackClasses( entitySource.getJpaCallbackClasses() ); if ( entitySource.getSynchronizedTableNames() != null ) { entityBinding.addSynchronizedTableNames( entitySource.getSynchronizedTableNames() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/AbstractTableSpecification.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/AbstractTableSpecification.java index e6d70e2861..2aad0d7bb1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/AbstractTableSpecification.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/AbstractTableSpecification.java @@ -40,7 +40,7 @@ public abstract class AbstractTableSpecification implements TableSpecification { private final int tableNumber; private final List valueList = new ArrayList(); - private final LinkedHashMap valueMap = new LinkedHashMap(); + private final LinkedHashMap valueMap = new LinkedHashMap(); private final PrimaryKey primaryKey = new PrimaryKey( this ); private final List foreignKeys = new ArrayList(); @@ -61,20 +61,23 @@ public abstract class AbstractTableSpecification implements TableSpecification { @Override public Column locateOrCreateColumn(String name) { - if ( valueMap.containsKey( name ) ) { - return (Column) valueMap.get( name ); + Column column = locateColumn( name ); + if(column == null){ + column = createColumn( name ); } - return createColumn( name ); + return column; } @Override public Column locateColumn(String name) { - if ( valueMap.containsKey( name ) ) { - return (Column) valueMap.get( name ); + final Identifier identifier = Identifier.toIdentifier( name ); + if ( valueMap.containsKey( identifier ) ) { + return (Column) valueMap.get( identifier ); } return null; } + @Override public Column createColumn(String name) { return createColumn( Identifier.toIdentifier( name ) ); @@ -83,18 +86,19 @@ public abstract class AbstractTableSpecification implements TableSpecification { @Override public Column createColumn(Identifier name) { final Column column = new Column( this, valueList.size(), name ); - valueMap.put( name.getText(), column ); + valueMap.put( name, column ); valueList.add( column ); return column; } @Override public DerivedValue locateOrCreateDerivedValue(String fragment) { - if ( valueMap.containsKey( fragment ) ) { - return (DerivedValue) valueMap.get( fragment ); + final Identifier identifier = Identifier.toIdentifier( fragment ); + if ( valueMap.containsKey( identifier ) ) { + return (DerivedValue) valueMap.get( identifier ); } final DerivedValue value = new DerivedValue( this, valueList.size(), fragment ); - valueMap.put( fragment, value ); + valueMap.put( identifier, value ); valueList.add( value ); return value; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/EntityTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/EntityTest.java index ad2d7e7d06..32d7c6d06d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/EntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/EntityTest.java @@ -30,8 +30,6 @@ import java.util.GregorianCalendar; import java.util.List; import java.util.TimeZone; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.hibernate.HibernateException; @@ -39,9 +37,7 @@ import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.StaleStateException; import org.hibernate.Transaction; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.type.StandardBasicTypes; import static org.junit.Assert.assertEquals; @@ -52,14 +48,17 @@ import static org.junit.Assert.fail; /** * @author Emmanuel Bernard */ -@FailureExpectedWithNewMetamodel public class EntityTest extends BaseCoreFunctionalTestCase { private DateFormat df = SimpleDateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); - + protected boolean isCleanupTestDataRequired() { + return true; + } @Test public void testLoad() throws Exception { //put an object in DB - assertEquals( "Flight", getEntityBinding( Flight.class ).getPrimaryTableName() ); + if ( isMetadataUsed ) { + assertEquals( "Flight", getEntityBinding( Flight.class ).getPrimaryTableName() ); + } Session s = openSession(); Transaction tx = s.beginTransaction(); @@ -314,7 +313,9 @@ public class EntityTest extends BaseCoreFunctionalTestCase { @Test public void testEntityName() throws Exception { - assertEquals( "Corporation", getEntityBinding( Company.class ).getPrimaryTableName() ); + if ( isMetadataUsed ) { + assertEquals( "Corporation", getEntityBinding( Company.class ).getPrimaryTableName() ); + } Session s = openSession(); Transaction tx = s.beginTransaction(); Company comp = new Company(); @@ -421,27 +422,5 @@ public class EntityTest extends BaseCoreFunctionalTestCase { Sky.class }; } - - // tests are leaving data around, so drop/recreate schema for now. this is wha the old tests did - - @Override - protected boolean createSchema() { - return false; - } - - @Before - public void runCreateSchema() { - schemaExport().create( false, true ); - } - - private SchemaExport schemaExport() { - return new SchemaExport( serviceRegistry(), configuration() ); - } - - @After - public void runDropSchema() { - schemaExport().drop( false, true ); - } - } diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/bag/PersistentBagTest.java b/hibernate-core/src/test/java/org/hibernate/test/collection/bag/PersistentBagTest.java index c0aa2508f9..ac01a7054d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/bag/PersistentBagTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/bag/PersistentBagTest.java @@ -29,7 +29,6 @@ import java.util.ArrayList; import org.hibernate.Session; import org.hibernate.collection.internal.PersistentBag; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; @@ -38,7 +37,6 @@ import org.junit.Test; * * @author Steve Ebersole */ -@FailureExpectedWithNewMetamodel( jiraKey = "HHH-7680" ) public class PersistentBagTest extends BaseCoreFunctionalTestCase { @Override public String[] getMappings() { diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/list/PersistentListTest.java b/hibernate-core/src/test/java/org/hibernate/test/collection/list/PersistentListTest.java index 2c2a4d20d7..009cfc363c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/list/PersistentListTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/list/PersistentListTest.java @@ -42,7 +42,6 @@ import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.QueryableCollection; import org.hibernate.sql.SimpleSelect; import org.hibernate.testing.FailureExpected; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; @@ -52,7 +51,6 @@ import org.junit.Test; * * @author Steve Ebersole */ -@FailureExpectedWithNewMetamodel( jiraKey = "HHH-7680" ) public class PersistentListTest extends BaseCoreFunctionalTestCase { @Override public String[] getMappings() { diff --git a/hibernate-core/src/test/java/org/hibernate/test/reattachment/CollectionReattachmentTest.java b/hibernate-core/src/test/java/org/hibernate/test/reattachment/CollectionReattachmentTest.java index 8c9e5b0e2b..3f1bd50816 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/reattachment/CollectionReattachmentTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/reattachment/CollectionReattachmentTest.java @@ -24,7 +24,6 @@ package org.hibernate.test.reattachment; import org.hibernate.Session; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; @@ -33,7 +32,6 @@ import org.junit.Test; * * @author Steve Ebersole */ -@FailureExpectedWithNewMetamodel( jiraKey = "HHH-7680" ) public class CollectionReattachmentTest extends BaseCoreFunctionalTestCase { @Override public String[] getMappings() { diff --git a/hibernate-core/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java b/hibernate-core/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java index ddd83f2a6a..56bbf343f6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java @@ -30,7 +30,6 @@ import java.util.Iterator; import java.util.Set; import org.hibernate.Session; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; @@ -39,7 +38,6 @@ import org.junit.Test; * * @author Steve Ebersole */ -@FailureExpectedWithNewMetamodel( jiraKey = "HHH-7680" ) public class ProxyReattachmentTest extends BaseCoreFunctionalTestCase { @Override public String[] getMappings() { @@ -100,7 +98,6 @@ public class ProxyReattachmentTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings( {"unchecked"}) - @FailureExpectedWithNewMetamodel public void testIterateWithClearTopOfLoop() { Session s = openSession(); s.beginTransaction(); @@ -142,7 +139,6 @@ public class ProxyReattachmentTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings( {"unchecked"}) - @FailureExpectedWithNewMetamodel public void testIterateWithClearBottomOfLoop() { Session s = openSession(); s.beginTransaction(); @@ -184,7 +180,6 @@ public class ProxyReattachmentTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings( {"unchecked"}) - @FailureExpectedWithNewMetamodel public void testIterateWithEvictTopOfLoop() { Session s = openSession(); s.beginTransaction(); @@ -222,7 +217,6 @@ public class ProxyReattachmentTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings( {"unchecked"}) - @FailureExpectedWithNewMetamodel public void testIterateWithEvictBottomOfLoop() { Session s = openSession(); s.beginTransaction(); diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java index 0a0f06bf66..648cbf82ce 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java @@ -89,7 +89,7 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { public static final Dialect DIALECT = Dialect.getDialect(); - private boolean isMetadataUsed; + protected boolean isMetadataUsed; private Configuration configuration; private MetadataImplementor metadataImplementor; private StandardServiceRegistryImpl serviceRegistry; @@ -502,7 +502,11 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { sessionFactory.getCache().evictNaturalIdRegions(); } } - protected boolean isCleanupTestDataRequired(){return false;} + + protected boolean isCleanupTestDataRequired() { + return false; + } + protected void cleanupTestData() throws Exception { Session s = openSession(); s.beginTransaction();