From 2115f1f79ce92f2f577d429093695f4620b9ac1a Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Thu, 16 Feb 2012 16:33:43 -0800 Subject: [PATCH] HHH-7050 : back out of changes --- .../metamodel/internal/source/Binder.java | 27 +++--- .../annotations/global/TableProcessor.java | 2 +- .../metamodel/spi/binding/EntityBinding.java | 6 +- .../metamodel/spi/relational/InLineView.java | 5 -- .../metamodel/spi/relational/Schema.java | 68 ++------------ .../metamodel/spi/relational/Table.java | 90 +++---------------- .../spi/relational/TableSpecification.java | 7 -- .../spi/binding/SimpleValueBindingTests.java | 2 +- .../relational/TableManipulationTests.java | 78 ++-------------- 9 files changed, 45 insertions(+), 240 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/Binder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/Binder.java index aa9216b274..96e93451b2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/Binder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/Binder.java @@ -256,7 +256,6 @@ public class Binder { bindSecondaryTables( entitySource, entityBinding ); Deque tableStack = new ArrayDeque( ); - tableStack.add( entityBinding.getPrimaryTable() ); bindAttributes( entitySource, entityBinding, tableStack ); bindTableUniqueConstraints( entitySource, entityBinding ); @@ -385,6 +384,7 @@ public class Binder { final EntityBinding entityBinding = buildBasicEntityBinding( entitySource, superEntityBinding ); entityBinding.setPrimaryTable( superEntityBinding.getPrimaryTable() ); + entityBinding.setPrimaryTableName( superEntityBinding.getPrimaryTableName() ); bindDiscriminatorValue( entitySource, entityBinding ); return entityBinding; @@ -989,10 +989,12 @@ public class Binder { final String tableName = attributeSource.getExplicitCollectionTableName(); if ( StringHelper.isNotEmpty( tableName ) ) { - final String table = currentBindingContext.getNamingStrategy().tableName( tableName ); - Table collectionTable = schema.locateTable( table ); + final Identifier tableIdentifier = Identifier.toIdentifier( + currentBindingContext.getNamingStrategy().tableName( tableName ) + ); + Table collectionTable = schema.locateTable( tableIdentifier ); if ( collectionTable == null ) { - collectionTable = schema.createTable( Identifier.toIdentifier( tableName ), true ); + collectionTable = schema.createTable( tableIdentifier ); } pluralAttributeBinding.setCollectionTable( collectionTable ); } @@ -1000,7 +1002,7 @@ public class Binder { // todo : not sure wel have all the needed info here in all cases, specifically when needing to know the "other side" final EntityBinding owner = pluralAttributeBinding.getContainer().seekEntityBinding(); final String ownerTableLogicalName = Table.class.isInstance( owner.getPrimaryTable() ) - ? owner.getPrimaryTable().getLogicalName() + ? Table.class.cast( owner.getPrimaryTable() ).getTableName().getName() : null; String collectionTableName = currentBindingContext.getNamingStrategy().collectionTableName( owner.getEntity().getName(), @@ -1010,13 +1012,11 @@ public class Binder { pluralAttributeBinding.getContainer().getPathBase() + '.' + attributeSource.getName() ); collectionTableName = quoteIdentifier( collectionTableName ); - // TODO: may not know physical collection table name if this is an association; - // for now, assume that the physical collection table name is known at this point; - boolean isPhysicalCollectionTable = true; pluralAttributeBinding.setCollectionTable( schema.locateOrCreateTable( - collectionTableName, - isPhysicalCollectionTable + Identifier.toIdentifier( + collectionTableName + ) ) ); } @@ -1037,9 +1037,11 @@ public class Binder { AbstractPluralAttributeBinding pluralAttributeBinding, Deque tableStack) { +// TableSpecification targetTable = tableStack.peekLast(); pluralAttributeBinding.getPluralAttributeKeyBinding().prepareForeignKey( attributeSource.getKeySource().getExplicitForeignKeyName(), - tableStack.peekLast().getLogicalName() + //tableStack.peekLast().getLogicalName() + null // todo : handle secondary table names ); pluralAttributeBinding.getPluralAttributeKeyBinding().getForeignKey().setDeleteRule( attributeSource.getKeySource().getOnDeleteAction() @@ -1289,6 +1291,7 @@ public class Binder { final TableSource tableSource = entitySource.getPrimaryTable(); final Table table = createTable( entityBinding, tableSource ); entityBinding.setPrimaryTable( table ); + entityBinding.setPrimaryTableName( table.getTableName().getName() ); } private void bindSecondaryTables(EntitySource entitySource, EntityBinding entityBinding) { @@ -1317,7 +1320,7 @@ public class Binder { return currentBindingContext.getMetadataImplementor() .getDatabase() .locateSchema( databaseSchemaName ) - .locateOrCreateTable( tableName, true ); + .locateOrCreateTable( Identifier.toIdentifier( tableName ) ); } private void bindTableUniqueConstraints(EntitySource entitySource, EntityBinding entityBinding) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/global/TableProcessor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/global/TableProcessor.java index 479c5ffab9..aff73c0eb6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/global/TableProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/global/TableProcessor.java @@ -81,7 +81,7 @@ public class TableProcessor { String tableName = JandexHelper.getValue( tableAnnotation, "appliesTo", String.class ); ObjectName objectName = new ObjectName( tableName ); Schema schema = metadata.getDatabase().getSchema( objectName.getSchema(), objectName.getCatalog() ); - Table table = schema.locateTable( tableName ); + Table table = schema.locateTable( objectName.getName() ); if ( table != null ) { bindHibernateTableAnnotation( table, tableAnnotation ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/EntityBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/EntityBinding.java index 378c5bc45f..272ff630d4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/EntityBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/EntityBinding.java @@ -231,7 +231,7 @@ public class EntityBinding implements AttributeBindingContainer { } public TableSpecification locateTable(String tableName) { - if ( tableName == null || tableName.equals( getPrimaryTableLogicalName() ) ) { + if ( tableName == null || tableName.equals( getPrimaryTableName() ) ) { return primaryTable; } TableSpecification tableSpec = secondaryTables.get( tableName ); @@ -246,8 +246,8 @@ public class EntityBinding implements AttributeBindingContainer { } return tableSpec; } - public String getPrimaryTableLogicalName() { - return primaryTable.getLogicalName(); + public String getPrimaryTableName() { + return primaryTableName; } public void setPrimaryTableName(String primaryTableName) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/InLineView.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/InLineView.java index f2381cf6ed..ba6d4ffa50 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/InLineView.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/InLineView.java @@ -53,11 +53,6 @@ public class InLineView extends AbstractTableSpecification { return select; } - @Override - public String getLogicalName() { - return logicalName; - } - @Override public String getLoggableValueQualifier() { return logicalName; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/Schema.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/Schema.java index 626073452d..fbc110d8d2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/Schema.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/Schema.java @@ -34,7 +34,7 @@ import java.util.Map; public class Schema { private final Name name; private Map inLineViews = new HashMap(); - private Map tables = new HashMap(); + private Map tables = new HashMap(); public Schema(Name name) { this.name = name; @@ -48,72 +48,20 @@ public class Schema { return name; } - /** - * Returns the table with the specified logical table name. - * . - * @param logicalTableName - * - * @return the table with the specified logical table name, - * or null if there is no table with the specified - * logical table name. - */ - public Table locateTable(String logicalTableName) { - return tables.get( logicalTableName ); + public Table locateTable(Identifier name) { + return tables.get( name ); } - /** - * Creates a {@link Table} with the specified logical name. If - * {@code isPhysicalName} is true, then {@code name} is also - * the phycical table name. - * - * @param name - the name of the table - * @param isPhysicalName - true, if the name is known to be the - * physical name. - * @return the created table. - */ - public Table createTable(Identifier name, boolean isPhysicalName) { - Table table = new Table( this, name, isPhysicalName ); - tables.put( table.getLogicalName(), table ); + public Table createTable(Identifier name) { + Table table = new Table( this, name ); + tables.put( name, table ); return table; } - /* package-protected */ - void remapLogicalTableName(String oldName) { - Table table = tables.remove( oldName ); - if ( table == null ) { - throw new IllegalStateException( - String.format( - "Schema (%s) does not contain a table with logical name (%s) to remap.", - name, - oldName - ) - ); - } - tables.put( table.getLogicalName(), table ); - } - - /** - * Locates the table with the specified name; if none is found, - * a table with the specified name is created. - *

- * The value for {@code isPhysicalTableName} is ignored if a table - * is located with the specified name, and that name is defined - * as the physical table name (indicated by a non-null value returned - * by {@link Table#getTableName()}. - * - * @param name - the name - * @param isPhysicalTableName - true, if the table is known to be - * the physical table name. - * @return the located or created table. - */ - public Table locateOrCreateTable(String name, boolean isPhysicalTableName) { + public Table locateOrCreateTable(Identifier name) { final Table existing = locateTable( name ); - Identifier tableIdentifier = Identifier.toIdentifier( name ); if ( existing == null ) { - return createTable( tableIdentifier, isPhysicalTableName ); - } - else if ( isPhysicalTableName && existing.getTableName() == null ) { - existing.setPhysicalName( tableIdentifier ); + return createTable( name ); } return existing; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/Table.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/Table.java index a0db4a0936..41ac427b9a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/Table.java @@ -34,108 +34,38 @@ import org.hibernate.dialect.Dialect; * * @author Gavin King * @author Steve Ebersole - * @author Gail Badner */ public class Table extends AbstractTableSpecification implements Exportable { private final Schema database; - private String logicalName; - private Identifier name; - private boolean isPhysicalName; - private ObjectName objectName; - private String qualifiedName; + private final Identifier tableName; + private final ObjectName objectName; + private final String qualifiedName; private final LinkedHashMap indexes = new LinkedHashMap(); private final LinkedHashMap uniqueKeys = new LinkedHashMap(); private final List checkConstraints = new ArrayList(); private final List comments = new ArrayList(); - /** - * Constructs a {@link Table} instance. - * - * @param database - the schema - * @param name - the name - * @param isPhysicalTableName - true, if the name is known to be the physical - * table name; false, otherwise. - */ - public Table(Schema database, String name, boolean isPhysicalTableName) { - this( database, Identifier.toIdentifier( name ), isPhysicalTableName ); + public Table(Schema database, String tableName) { + this( database, Identifier.toIdentifier( tableName ) ); } - - /** - * Constructs a {@link Table} instance. - * - * @param database - the schema - * @param name - the name {@link Identifier} - * @param isPhysicalTableName - true, if the name is known to be the physical - * table name; false, otherwise. - */ - public Table(Schema database, Identifier name, boolean isPhysicalTableName) { + public Table(Schema database, Identifier tableName) { this.database = database; - setName( name, isPhysicalTableName ); + this.tableName = tableName; + objectName = new ObjectName( database.getName().getSchema(), database.getName().getCatalog(), tableName ); + this.qualifiedName = objectName.toText(); } - private static String createLogicalName(Identifier tableName) { - return tableName.isQuoted() ? - new StringBuilder( tableName.getName().length() + 2 ) - .append( '`' ) - .append( tableName.getName() ) - .append( '`' ).toString() : - tableName.getName(); - } - @Override public Schema getSchema() { return database; } - /** - * Gets the logical table name. - * - * @return the logical table name. - */ - public String getLogicalName() { - return logicalName; - } - - /** - * Returns the physical table name. - * - * @return the physical table name, or null if the physical - * table name is not defined. - */ public Identifier getTableName() { - return isPhysicalName ? name : null; + return tableName; } - /** - * Sets the physical table name. - * - * @param physicalName - the physical table name - */ - public final void setPhysicalName(Identifier physicalName) { - setName( physicalName, true ); - } - - private void setName(Identifier name, boolean isPhysicalName) { - if ( name == null ) { - throw new IllegalArgumentException( "name cannot be null." ); - } - if ( isPhysicalName && ! this.isPhysicalName ) { - this.isPhysicalName = isPhysicalName; - } - if ( ! name.equals( this.name ) ) { - String logicalNameOld = logicalName; - this.name = name; - this.logicalName = createLogicalName( name ); - objectName = new ObjectName( database.getName().getSchema(), database.getName().getCatalog(), name ); - this.qualifiedName = objectName.toText(); - if ( logicalNameOld != null ) { - database.remapLogicalTableName( logicalNameOld ); - } - } - } - @Override public String getLoggableValueQualifier() { return qualifiedName; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/TableSpecification.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/TableSpecification.java index e8163280fc..2e17804b02 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/TableSpecification.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/relational/TableSpecification.java @@ -45,13 +45,6 @@ public interface TableSpecification extends ValueContainer, Loggable { */ public int getTableNumber(); - /** - * Get logical table name - * - * @return the logical table name. - */ - public String getLogicalName(); - /** * Get the primary key definition for this table spec. * diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/SimpleValueBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/SimpleValueBindingTests.java index ed11f8edc8..23ae70504d 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/SimpleValueBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/SimpleValueBindingTests.java @@ -56,7 +56,7 @@ public class SimpleValueBindingTests extends BaseUnitTestCase { @Test public void testBasicMiddleOutBuilding() { - Table table = new Table( new Schema( null, null ), "the_table", true ); + Table table = new Table( new Schema( null, null ), "the_table" ); Column idColumn = table.locateOrCreateColumn( "id" ); idColumn.setJdbcDataType( BIGINT ); idColumn.setSize( Size.precision( 18, 0 ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/spi/relational/TableManipulationTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/spi/relational/TableManipulationTests.java index f6091d507f..6d62810152 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/spi/relational/TableManipulationTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/spi/relational/TableManipulationTests.java @@ -35,7 +35,6 @@ import org.hibernate.testing.junit4.BaseUnitTestCase; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; /** @@ -48,7 +47,7 @@ public class TableManipulationTests extends BaseUnitTestCase { @Test public void testTableCreation() { Schema schema = new Schema( null, null ); - Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), true ); + Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) ); assertNull( table.getSchema().getName().getSchema() ); assertNull( table.getSchema().getName().getCatalog() ); assertEquals( "my_table", table.getTableName().toString() ); @@ -92,10 +91,10 @@ public class TableManipulationTests extends BaseUnitTestCase { @Test public void testTableSpecificationCounter() { Schema schema = new Schema( null, null ); - Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), true ); + Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) ); InLineView inLineView = schema.createInLineView( "my_inlineview", "subselect" ); InLineView otherInLineView = schema.createInLineView( "my_other_inlineview", "other subselect" ); - Table otherTable = schema.createTable( Identifier.toIdentifier( "my_other_table" ), true ); + Table otherTable = schema.createTable( Identifier.toIdentifier( "my_other_table" ) ); int firstTableNumber = table.getTableNumber(); assertEquals( firstTableNumber, table.getTableNumber() ); @@ -107,7 +106,7 @@ public class TableManipulationTests extends BaseUnitTestCase { @Test public void testBasicForeignKeyDefinition() { Schema schema = new Schema( null, null ); - Table book = schema.createTable( Identifier.toIdentifier( "BOOK" ), true ); + Table book = schema.createTable( Identifier.toIdentifier( "BOOK" ) ); Column bookId = book.locateOrCreateColumn( "id" ); bookId.setJdbcDataType( INTEGER ); @@ -115,7 +114,7 @@ public class TableManipulationTests extends BaseUnitTestCase { book.getPrimaryKey().addColumn( bookId ); book.getPrimaryKey().setName( "BOOK_PK" ); - Table page = schema.createTable( Identifier.toIdentifier( "PAGE" ), true ); + Table page = schema.createTable( Identifier.toIdentifier( "PAGE" ) ); Column pageId = page.locateOrCreateColumn( "id" ); pageId.setJdbcDataType( INTEGER ); @@ -137,12 +136,12 @@ public class TableManipulationTests extends BaseUnitTestCase { public void testQualifiedName() { Dialect dialect = new H2Dialect(); Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); - Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), true ); + Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) ); assertEquals( "my_table", table.getTableName().getName() ); assertEquals( "my_table", table.getTableName().toString() ); assertEquals( "schema.\"catalog\".my_table", table.getQualifiedName( dialect ) ); - table = schema.createTable( Identifier.toIdentifier( "`my_table`" ), true ); + table = schema.createTable( Identifier.toIdentifier( "`my_table`" ) ); assertEquals( "my_table", table.getTableName().getName() ); assertEquals( "`my_table`", table.getTableName().toString() ); assertEquals( "schema.\"catalog\".\"my_table\"", table.getQualifiedName( dialect ) ); @@ -150,67 +149,4 @@ public class TableManipulationTests extends BaseUnitTestCase { InLineView inLineView = schema.createInLineView( "my_inlineview", "select ..." ); assertEquals( "( select ... )", inLineView.getQualifiedName( dialect ) ); } - - @Test - public void testTableLogicalName() { - Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); - Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), false ); - assertEquals( "my_table", table.getLogicalName() ); - assertNull( table.getTableName() ); - assertSame( table, schema.locateTable( table.getLogicalName() ) ); - table.setPhysicalName( Identifier.toIdentifier( "my_new_table" ) ); - assertEquals( "my_new_table", table.getLogicalName() ); - assertEquals( "my_new_table", table.getTableName().getName() ); - assertSame( table, schema.locateTable( "my_new_table" ) ); - assertNull( schema.locateTable( "my_table" ) ); - table.setPhysicalName( Identifier.toIdentifier( "my_newer_table" ) ); - assertEquals( "my_newer_table", table.getLogicalName() ); - assertEquals( "my_newer_table", table.getTableName().getName() ); - assertSame( table, schema.locateTable( "my_newer_table" ) ); - assertNull( schema.locateTable( "my_new_table" ) ); - } - - @Test - public void testQuotedTableLogicalName() { - Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); - Table table = schema.createTable( Identifier.toIdentifier( "`my_table`" ), false ); - assertEquals( "`my_table`", table.getLogicalName() ); - assertNull( table.getTableName() ); - assertSame( table, schema.locateTable( table.getLogicalName() ) ); - table.setPhysicalName( Identifier.toIdentifier( "`my_new_table`" ) ); - assertEquals( "`my_new_table`", table.getLogicalName() ); - assertEquals( "my_new_table", table.getTableName().getName() ); - assertSame( table, schema.locateTable( "`my_new_table`" ) ); - assertNull( schema.locateTable( "`my_table`" ) ); - table.setPhysicalName( Identifier.toIdentifier( "`my_newer_table`" ) ); - assertEquals( "`my_newer_table`", table.getLogicalName() ); - assertEquals( "my_newer_table", table.getTableName().getName() ); - assertSame( table, schema.locateTable( "`my_newer_table`" ) ); - assertNull( schema.locateTable( "`my_new_table`" ) ); - } - - @Test - public void testInLineViewLogicalName() { - Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); - InLineView view = schema.createInLineView( "my_view", "select" ); - assertEquals( "my_view", view.getLogicalName() ); - assertEquals( "select", view.getSelect() ); - assertSame( view, schema.getInLineView( view.getLogicalName() ) ); - } - - @Test - public void testLocateOrCreateTable() { - Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); - Table table = schema.locateOrCreateTable( "my_table", false ); - assertEquals( "my_table", table.getLogicalName() ); - assertNull( table.getTableName() ); - Table tableTemp = schema.locateOrCreateTable( "my_table", true ); - assertSame( table, tableTemp ); - assertEquals( "my_table", table.getLogicalName() ); - assertEquals( Identifier.toIdentifier( "my_table" ), table.getTableName() ); - tableTemp = schema.locateOrCreateTable( "my_table", false ); - assertSame( table, tableTemp ); - assertEquals( "my_table", table.getLogicalName() ); - assertEquals( Identifier.toIdentifier( "my_table" ), table.getTableName() ); - } }