mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-26 05:14:57 +00:00
HHH-9713 - Creating an index in the database incorrectly tries to qualify the index with table name for some databases
This commit is contained in:
parent
b7273d93a2
commit
a96385a6e0
@ -28,8 +28,8 @@
|
||||
/**
|
||||
* @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 AbstractQualifiedName(Schema.Name schemaName, Identifier objectName) {
|
||||
);
|
||||
}
|
||||
|
||||
public AbstractQualifiedName(Identifier catalogName, Identifier schemaName, Identifier objectName) {
|
||||
public QualifiedNameImpl(Identifier catalogName, Identifier schemaName, Identifier objectName) {
|
||||
super( catalogName, schemaName, objectName );
|
||||
}
|
||||
}
|
@ -28,7 +28,7 @@
|
||||
/**
|
||||
* @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 );
|
||||
}
|
||||
|
@ -28,7 +28,7 @@
|
||||
/**
|
||||
* @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 );
|
||||
}
|
||||
|
@ -183,10 +183,10 @@ public Identifier fromMetaDataCatalogName(String catalogName) {
|
||||
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 Identifier fromMetaDataSchemaName(String schemaName) {
|
||||
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 );
|
||||
}
|
||||
|
@ -141,7 +141,8 @@ private String determineCatalogFilter(Identifier catalog) throws SQLException {
|
||||
// }
|
||||
|
||||
if ( identifierToUse == null ) {
|
||||
return SANS_CATALOG_FILTER;
|
||||
return ALL_CATALOGS_FILTER;
|
||||
// return SANS_CATALOG_FILTER;
|
||||
}
|
||||
|
||||
return determineAppropriateCapitalization( identifierToUse );
|
||||
@ -157,7 +158,8 @@ private String determineSchemaFilter(Identifier schema) throws SQLException {
|
||||
// }
|
||||
|
||||
if ( identifierToUse == null ) {
|
||||
return SANS_SCHEMA_FILTER;
|
||||
return ALL_SCHEMAS_FILTER;
|
||||
// return SANS_SCHEMA_FILTER;
|
||||
}
|
||||
|
||||
return determineAppropriateCapitalization( identifierToUse );
|
||||
@ -196,19 +198,13 @@ else if ( extractionContext.getJdbcDatabaseMetaData().storesLowerCaseIdentifiers
|
||||
|
||||
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 ),
|
||||
|
@ -26,6 +26,7 @@
|
||||
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 String[] getSqlCreateStrings(Index index, Metadata metadata) {
|
||||
|
||||
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();
|
||||
|
@ -33,6 +33,7 @@
|
||||
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 void basicTests() {
|
||||
}
|
||||
}
|
||||
|
||||
@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 );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 void testSimpleColumnAddition() {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user