From a96385a6e0255d9a3ca1e3fda07bde23275827d4 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 6 Apr 2015 13:47:48 -0500 Subject: [PATCH] HHH-9713 - Creating an index in the database incorrectly tries to qualify the index with table name for some databases --- ...lifiedName.java => QualifiedNameImpl.java} | 6 ++-- .../relational/QualifiedSequenceName.java | 2 +- .../model/relational/QualifiedTableName.java | 2 +- .../NormalizingIdentifierHelperImpl.java | 16 ++++----- ...tionExtractorJdbcDatabaseMetaDataImpl.java | 18 ++++------ .../internal/StandardIndexExporter.java | 10 +++++- .../disabled/DisabledForeignKeyTest.java | 32 +++++++++++++++++ .../test/schemaupdate/MigrationTest.java | 36 +++++++++++++++++++ 8 files changed, 97 insertions(+), 25 deletions(-) rename hibernate-core/src/main/java/org/hibernate/boot/model/relational/{AbstractQualifiedName.java => QualifiedNameImpl.java} (82%) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/AbstractQualifiedName.java b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedNameImpl.java similarity index 82% rename from hibernate-core/src/main/java/org/hibernate/boot/model/relational/AbstractQualifiedName.java rename to hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedNameImpl.java index 08d8a96b35..c0467dcb6a 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/AbstractQualifiedName.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedNameImpl.java @@ -28,8 +28,8 @@ import org.hibernate.boot.model.naming.Identifier; /** * @author Steve Ebersole */ -public class AbstractQualifiedName extends QualifiedNameParser.NameParts implements QualifiedName { - public AbstractQualifiedName(Schema.Name schemaName, Identifier objectName) { +public class QualifiedNameImpl extends QualifiedNameParser.NameParts implements QualifiedName { + public QualifiedNameImpl(Schema.Name schemaName, Identifier objectName) { this( schemaName.getCatalog(), schemaName.getSchema(), @@ -37,7 +37,7 @@ public class AbstractQualifiedName extends QualifiedNameParser.NameParts impleme ); } - public AbstractQualifiedName(Identifier catalogName, Identifier schemaName, Identifier objectName) { + public QualifiedNameImpl(Identifier catalogName, Identifier schemaName, Identifier objectName) { super( catalogName, schemaName, objectName ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedSequenceName.java b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedSequenceName.java index 2ee9e0dbfd..bcb6905b7c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedSequenceName.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedSequenceName.java @@ -28,7 +28,7 @@ import org.hibernate.boot.model.naming.Identifier; /** * @author Steve Ebersole */ -public class QualifiedSequenceName extends AbstractQualifiedName { +public class QualifiedSequenceName extends QualifiedNameImpl { public QualifiedSequenceName(Identifier catalogName, Identifier schemaName, Identifier sequenceName) { super( catalogName, schemaName, sequenceName ); } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedTableName.java b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedTableName.java index 31434435ff..4721c72bfc 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedTableName.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/QualifiedTableName.java @@ -28,7 +28,7 @@ import org.hibernate.boot.model.naming.Identifier; /** * @author Steve Ebersole */ -public class QualifiedTableName extends AbstractQualifiedName { +public class QualifiedTableName extends QualifiedNameImpl { public QualifiedTableName(Identifier catalogName, Identifier schemaName, Identifier tableName) { super( catalogName, schemaName, tableName ); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java index 5d60075bdd..f71ea6c08c 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java @@ -183,10 +183,10 @@ public class NormalizingIdentifierHelperImpl implements IdentifierHelper { return null; } - if ( jdbcEnvironment.getCurrentCatalog() == null - || catalogName.equals( jdbcEnvironment.getCurrentCatalog().getText() ) ) { - return null; - } +// if ( jdbcEnvironment.getCurrentCatalog() == null +// || catalogName.equals( jdbcEnvironment.getCurrentCatalog().getText() ) ) { +// return null; +// } return toIdentifierFromMetaData( catalogName ); } @@ -227,10 +227,10 @@ public class NormalizingIdentifierHelperImpl implements IdentifierHelper { return null; } - if ( jdbcEnvironment.getCurrentSchema() == null - || schemaName.equals( jdbcEnvironment.getCurrentSchema().getText() ) ) { - return null; - } +// if ( jdbcEnvironment.getCurrentSchema() == null +// || schemaName.equals( jdbcEnvironment.getCurrentSchema().getText() ) ) { +// return null; +// } return toIdentifier( schemaName ); } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/InformationExtractorJdbcDatabaseMetaDataImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/InformationExtractorJdbcDatabaseMetaDataImpl.java index beedc587b9..2e142e137f 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/InformationExtractorJdbcDatabaseMetaDataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/InformationExtractorJdbcDatabaseMetaDataImpl.java @@ -141,7 +141,8 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information // } if ( identifierToUse == null ) { - return SANS_CATALOG_FILTER; + return ALL_CATALOGS_FILTER; +// return SANS_CATALOG_FILTER; } return determineAppropriateCapitalization( identifierToUse ); @@ -157,7 +158,8 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information // } if ( identifierToUse == null ) { - return SANS_SCHEMA_FILTER; + return ALL_SCHEMAS_FILTER; +// return SANS_SCHEMA_FILTER; } return determineAppropriateCapitalization( identifierToUse ); @@ -196,19 +198,13 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information public TableInformation extractTableInformation(ResultSet resultSet) throws SQLException { final Identifier catalogIdentifier = identifierHelper().fromMetaDataCatalogName( - resultSet.getString( - "TABLE_CAT" - ) + resultSet.getString( "TABLE_CAT" ) ); final Identifier schemaIdentifier = identifierHelper().fromMetaDataSchemaName( - resultSet.getString( - "TABLE_SCHEM" - ) + resultSet.getString( "TABLE_SCHEM" ) ); final Identifier tableIdentifier = identifierHelper().fromMetaDataObjectName( - resultSet.getString( - "TABLE_NAME" - ) + resultSet.getString( "TABLE_NAME" ) ); final QualifiedTableName tableName = new QualifiedTableName( new Schema.Name( catalogIdentifier, schemaIdentifier ), diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardIndexExporter.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardIndexExporter.java index f9d0b45db7..4a21127a87 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardIndexExporter.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardIndexExporter.java @@ -26,6 +26,7 @@ package org.hibernate.tool.schema.internal; import java.util.Iterator; import org.hibernate.boot.Metadata; +import org.hibernate.boot.model.relational.QualifiedNameImpl; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.internal.util.StringHelper; @@ -53,7 +54,14 @@ public class StandardIndexExporter implements Exporter { final String indexNameForCreation; if ( dialect.qualifyIndexName() ) { - indexNameForCreation = StringHelper.qualify( tableName, index.getName() ); + indexNameForCreation = jdbcEnvironment.getQualifiedObjectNameFormatter().format( + new QualifiedNameImpl( + index.getTable().getQualifiedTableName().getCatalogName(), + index.getTable().getQualifiedTableName().getSchemaName(), + jdbcEnvironment.getIdentifierHelper().toIdentifier( index.getName() ) + ), + jdbcEnvironment.getDialect() + ); } else { indexNameForCreation = index.getName(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/foreignkeys/disabled/DisabledForeignKeyTest.java b/hibernate-core/src/test/java/org/hibernate/test/foreignkeys/disabled/DisabledForeignKeyTest.java index 7e17480bd3..5899cbd584 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/foreignkeys/disabled/DisabledForeignKeyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/foreignkeys/disabled/DisabledForeignKeyTest.java @@ -33,6 +33,7 @@ import org.hibernate.mapping.ForeignKey; import org.hibernate.mapping.Table; import org.hibernate.mapping.Table.ForeignKeyKey; import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseUnitTestCase; @@ -82,4 +83,35 @@ public class DisabledForeignKeyTest extends BaseUnitTestCase { } } + @Test + @TestForIssue( jiraKey = "HHH-9704" ) + public void expandedTests() { + StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(); + StandardServiceRegistry standardRegistry = registryBuilder.build(); + try { + final MetadataSources sources = new MetadataSources( standardRegistry ); + + sources.addAnnotatedClass( ManyToManyOwner.class ); + sources.addAnnotatedClass( ManyToManyTarget.class ); + + final MetadataImplementor metadata = (MetadataImplementor) sources.buildMetadata(); + metadata.validate(); + + // export the schema + new SchemaExport( metadata ).execute( false, true, false, false ); + + try { + // update the schema + new SchemaUpdate( metadata ).execute( false, true ); + } + finally { + // drop the schema + new SchemaExport( metadata ).execute( false, true, false, false ); + } + } + finally { + StandardServiceRegistryBuilder.destroy( standardRegistry ); + } + } + } diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/MigrationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/MigrationTest.java index 0b7b8d8a8c..b89dfa5c21 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/MigrationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/MigrationTest.java @@ -23,13 +23,21 @@ */ package org.hibernate.test.schemaupdate; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Index; +import javax.persistence.Table; + import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.hbm2ddl.SchemaUpdate; +import org.hibernate.tool.hbm2ddl.Target; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseUnitTestCase; import org.junit.After; import org.junit.Before; @@ -130,5 +138,33 @@ public class MigrationTest extends BaseUnitTestCase { // new SchemaExport( serviceRegistry, v3cfg ).drop( false, true ); // } + + @Test + @TestForIssue( jiraKey = "HHH-9713" ) + public void testIndexCreationViaSchemaUpdate() { + MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) + .addAnnotatedClass( EntityWithIndex.class ) + .buildMetadata(); + + // export the schema + new SchemaExport( metadata ).execute( Target.EXPORT, SchemaExport.Type.CREATE ); + + try { + // update the schema + new SchemaUpdate( metadata ).execute( Target.EXPORT ); + } + finally { + // drop the schema + new SchemaExport( metadata ).execute( Target.EXPORT, SchemaExport.Type.DROP ); + } + } + + @Entity( name = "EntityWithIndex" ) + @Table( name = "T_Entity_With_Index",indexes = @Index( columnList = "name" ) ) + public static class EntityWithIndex { + @Id + public Integer id; + public String name; + } }