diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/Database.java b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/Database.java index f3779539f7..a8b50ede5c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/Database.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/Database.java @@ -37,7 +37,7 @@ public class Database { private final ServiceRegistry serviceRegistry; private final PhysicalNamingStrategy physicalNamingStrategy; - private Namespace implicitNamespace; + private Namespace.Name physicalImplicitNamespaceName; private List initCommands; public Database(MetadataBuildingOptions buildingOptions) { @@ -51,11 +51,16 @@ public class Database { this.physicalNamingStrategy = buildingOptions.getPhysicalNamingStrategy(); this.dialect = determineDialect( buildingOptions ); - this.implicitNamespace = makeNamespace( - new Namespace.Name( - toIdentifier( buildingOptions.getMappingDefaults().getImplicitCatalogName() ), - toIdentifier( buildingOptions.getMappingDefaults().getImplicitSchemaName() ) - ) + setImplicitNamespaceName( + toIdentifier( buildingOptions.getMappingDefaults().getImplicitCatalogName() ), + toIdentifier( buildingOptions.getMappingDefaults().getImplicitSchemaName() ) + ); + } + + private void setImplicitNamespaceName(Identifier catalogName, Identifier schemaName) { + this.physicalImplicitNamespaceName = new Namespace.Name( + physicalNamingStrategy.toPhysicalCatalogName( catalogName, jdbcEnvironment ), + physicalNamingStrategy.toPhysicalSchemaName( schemaName, jdbcEnvironment ) ); } @@ -111,15 +116,25 @@ public class Database { return namespaceMap.values(); } + /** + * @return The default namespace, with a {@code null} catalog and schema + * which will have to be interpreted with defaults at runtime. + * @see SqlStringGenerationContext + */ public Namespace getDefaultNamespace() { - return implicitNamespace; + return locateNamespace( null, null ); + } + + /** + * @return The implicit name of the default namespace, with a {@code null} catalog and schema + * which will have to be interpreted with defaults at runtime. + * @see SqlStringGenerationContext + */ + public Namespace.Name getPhysicalImplicitNamespaceName() { + return physicalImplicitNamespaceName; } public Namespace locateNamespace(Identifier catalogName, Identifier schemaName) { - if ( catalogName == null && schemaName == null ) { - return getDefaultNamespace(); - } - final Namespace.Name name = new Namespace.Name( catalogName, schemaName ); Namespace namespace = namespaceMap.get( name ); if ( namespace == null ) { @@ -129,17 +144,8 @@ public class Database { } public Namespace adjustDefaultNamespace(Identifier catalogName, Identifier schemaName) { - final Namespace.Name name = new Namespace.Name( catalogName, schemaName ); - if ( implicitNamespace.getName().equals( name ) ) { - return implicitNamespace; - } - - Namespace namespace = namespaceMap.get( name ); - if ( namespace == null ) { - namespace = makeNamespace( name ); - } - implicitNamespace = namespace; - return implicitNamespace; + setImplicitNamespaceName( catalogName, schemaName ); + return locateNamespace( catalogName, schemaName ); } public Namespace adjustDefaultNamespace(String implicitCatalogName, String implicitSchemaName) { diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/SqlStringGenerationContext.java b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/SqlStringGenerationContext.java index fcd682ae7e..d808e7c20e 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/SqlStringGenerationContext.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/SqlStringGenerationContext.java @@ -36,6 +36,12 @@ public interface SqlStringGenerationContext { */ Identifier getDefaultCatalog(); + /** + * @param explicitCatalogOrNull An explicitly configured catalog, or {@code null}. + * @return The given identifier if non-{@code null}, or the default catalog otherwise. + */ + Identifier catalogWithDefault(Identifier explicitCatalogOrNull); + /** * @return The default schema, used for table/sequence names that do not explicitly mention a schema. * May be {@code null}. @@ -44,6 +50,12 @@ public interface SqlStringGenerationContext { */ Identifier getDefaultSchema(); + /** + * @param explicitSchemaOrNull An explicitly configured schema, or {@code null}. + * @return The given identifier if non-{@code null}, or the default schema otherwise. + */ + Identifier schemaWithDefault(Identifier explicitSchemaOrNull); + /** * Render a formatted a table name * diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/internal/SqlStringGenerationContextImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/internal/SqlStringGenerationContextImpl.java index b692562e0c..05091caa25 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/relational/internal/SqlStringGenerationContextImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/relational/internal/SqlStringGenerationContextImpl.java @@ -47,7 +47,7 @@ public class SqlStringGenerationContextImpl */ public static SqlStringGenerationContext fromExplicit(JdbcEnvironment jdbcEnvironment, Database database, String defaultCatalog, String defaultSchema) { - Namespace.Name implicitNamespaceName = database.getDefaultNamespace().getPhysicalName(); + Namespace.Name implicitNamespaceName = database.getPhysicalImplicitNamespaceName(); IdentifierHelper identifierHelper = jdbcEnvironment.getIdentifierHelper(); NameQualifierSupport nameQualifierSupport = jdbcEnvironment.getNameQualifierSupport(); Identifier actualDefaultCatalog = null; @@ -107,11 +107,48 @@ public class SqlStringGenerationContextImpl return defaultCatalog; } + @Override + public Identifier catalogWithDefault(Identifier explicitCatalogOrNull) { + return explicitCatalogOrNull != null ? explicitCatalogOrNull : defaultCatalog; + } + @Override public Identifier getDefaultSchema() { return defaultSchema; } + @Override + public Identifier schemaWithDefault(Identifier explicitSchemaOrNull) { + return explicitSchemaOrNull != null ? explicitSchemaOrNull : defaultSchema; + } + + private QualifiedTableName withDefaults(QualifiedTableName name) { + if ( name.getCatalogName() == null && defaultCatalog != null + || name.getSchemaName() == null && defaultSchema != null ) { + return new QualifiedTableName( catalogWithDefault( name.getCatalogName() ), + schemaWithDefault( name.getSchemaName() ), name.getTableName() ); + } + return name; + } + + private QualifiedSequenceName withDefaults(QualifiedSequenceName name) { + if ( name.getCatalogName() == null && defaultCatalog != null + || name.getSchemaName() == null && defaultSchema != null ) { + return new QualifiedSequenceName( catalogWithDefault( name.getCatalogName() ), + schemaWithDefault( name.getSchemaName() ), name.getSequenceName() ); + } + return name; + } + + private QualifiedName withDefaults(QualifiedName name) { + if ( name.getCatalogName() == null && defaultCatalog != null + || name.getSchemaName() == null && defaultSchema != null ) { + return new QualifiedSequenceName( catalogWithDefault( name.getCatalogName() ), + schemaWithDefault( name.getSchemaName() ), name.getObjectName() ); + } + return name; + } + @Override public String format(QualifiedTableName qualifiedName) { return qualifiedObjectNameFormatter.format( withDefaults( qualifiedName ), dialect ); @@ -127,44 +164,13 @@ public class SqlStringGenerationContextImpl return qualifiedObjectNameFormatter.format( withDefaults( qualifiedName ), dialect ); } - private QualifiedTableName withDefaults(QualifiedTableName name) { - if ( name.getCatalogName() == null && defaultCatalog != null - || name.getSchemaName() == null && defaultSchema != null ) { - return new QualifiedTableName( withDefault( name.getCatalogName(), defaultCatalog ), - withDefault( name.getSchemaName(), defaultSchema ), name.getTableName() ); - } - return name; - } - - private QualifiedSequenceName withDefaults(QualifiedSequenceName name) { - if ( name.getCatalogName() == null && defaultCatalog != null - || name.getSchemaName() == null && defaultSchema != null ) { - return new QualifiedSequenceName( withDefault( name.getCatalogName(), defaultCatalog ), - withDefault( name.getSchemaName(), defaultSchema ), name.getSequenceName() ); - } - return name; - } - - private QualifiedName withDefaults(QualifiedName name) { - if ( name.getCatalogName() == null && defaultCatalog != null - || name.getSchemaName() == null && defaultSchema != null ) { - return new QualifiedSequenceName( withDefault( name.getCatalogName(), defaultCatalog ), - withDefault( name.getSchemaName(), defaultSchema ), name.getObjectName() ); - } - return name; - } - - private static Identifier withDefault(Identifier value, Identifier defaultValue) { - return value != null ? value : defaultValue; - } - @Override public String formatWithoutCatalog(QualifiedSequenceName qualifiedName) { QualifiedSequenceName nameToFormat; if ( qualifiedName.getCatalogName() != null || qualifiedName.getSchemaName() == null && defaultSchema != null ) { nameToFormat = new QualifiedSequenceName( null, - withDefault( qualifiedName.getSchemaName(), defaultSchema ), qualifiedName.getSequenceName() ); + schemaWithDefault( qualifiedName.getSchemaName() ), qualifiedName.getSequenceName() ); } else { nameToFormat = qualifiedName; diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java index 96a956e52a..f090e03cc3 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java @@ -796,15 +796,6 @@ public class ModelBinder { // YUCK! but cannot think of a clean way to do this given the string-config based scheme params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, objectNameNormalizer); - String implicitSchemaName = metadataBuildingContext.getMappingDefaults().getImplicitSchemaName(); - if ( implicitSchemaName != null ) { - params.setProperty( PersistentIdentifierGenerator.SCHEMA, implicitSchemaName ); - } - String implicitCatalogName = metadataBuildingContext.getMappingDefaults().getImplicitCatalogName(); - if ( implicitCatalogName != null ) { - params.setProperty( PersistentIdentifierGenerator.CATALOG, implicitCatalogName ); - } - params.putAll( generator.getParameters() ); identifierValue.setIdentifierGeneratorProperties( params ); @@ -3056,7 +3047,7 @@ public class ModelBinder { return database.toIdentifier( tableSpecSource.getExplicitSchemaName() ); } else { - return database.toIdentifier( metadataBuildingContext.getMappingDefaults().getImplicitSchemaName() ); + return null; } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index b3a28bb1f1..4c4b38539f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -100,7 +100,6 @@ import org.hibernate.cfg.annotations.TableBinder; import org.hibernate.cfg.internal.NullableDiscriminatorColumnSecondPass; import org.hibernate.engine.OptimisticLockStyle; import org.hibernate.engine.spi.FilterDefinition; -import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.StringHelper; import org.hibernate.jpa.event.internal.CallbackDefinitionResolverLegacyImpl; @@ -476,20 +475,6 @@ public final class AnnotationBinder { IdentifierGeneratorDefinition.Builder definitionBuilder = new IdentifierGeneratorDefinition.Builder(); - if ( context.getMappingDefaults().getImplicitSchemaName() != null ) { - definitionBuilder.addParam( - PersistentIdentifierGenerator.SCHEMA, - context.getMappingDefaults().getImplicitSchemaName() - ); - } - - if ( context.getMappingDefaults().getImplicitCatalogName() != null ) { - definitionBuilder.addParam( - PersistentIdentifierGenerator.CATALOG, - context.getMappingDefaults().getImplicitCatalogName() - ); - } - if ( generatorAnn instanceof TableGenerator ) { context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretTableGenerator( (TableGenerator) generatorAnn, diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java index dae34b03a0..5932a86327 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java @@ -528,15 +528,6 @@ public class BinderHelper { PersistentIdentifierGenerator.TABLE, table.getName() ); - final String implicitCatalogName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitCatalogName(); - if ( implicitCatalogName != null ) { - params.put( PersistentIdentifierGenerator.CATALOG, implicitCatalogName ); - } - final String implicitSchemaName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitSchemaName(); - if ( implicitSchemaName != null ) { - params.put( PersistentIdentifierGenerator.SCHEMA, implicitSchemaName ); - } - if ( id.getColumnSpan() == 1 ) { params.setProperty( PersistentIdentifierGenerator.PK, diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Settings.java b/hibernate-core/src/main/java/org/hibernate/cfg/Settings.java index 51ca0654b2..410a0bc925 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Settings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Settings.java @@ -47,8 +47,8 @@ public final class Settings { public Settings(SessionFactoryOptions sessionFactoryOptions, Metadata metadata) { this( sessionFactoryOptions, - extractName( metadata.getDatabase().getDefaultNamespace().getName().getCatalog() ), - extractName( metadata.getDatabase().getDefaultNamespace().getName().getSchema() ) + extractName( metadata.getDatabase().getPhysicalImplicitNamespaceName().getCatalog() ), + extractName( metadata.getDatabase().getPhysicalImplicitNamespaceName().getSchema() ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java index b4161bed01..829d6cdd29 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java @@ -480,10 +480,10 @@ public class TableBinder { String subselect, InFlightMetadataCollector.EntityTableXref denormalizedSuperTableXref) { schema = BinderHelper.isEmptyOrNullAnnotationValue( schema ) - ? buildingContext.getBuildingOptions().getMappingDefaults().getImplicitSchemaName() + ? null : schema; catalog = BinderHelper.isEmptyOrNullAnnotationValue( catalog ) - ? buildingContext.getBuildingOptions().getMappingDefaults().getImplicitCatalogName() + ? null : catalog; final Table table; diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/JPAXMLOverriddenAnnotationReader.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/JPAXMLOverriddenAnnotationReader.java index eec2853393..1c25c84510 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/JPAXMLOverriddenAnnotationReader.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/JPAXMLOverriddenAnnotationReader.java @@ -407,7 +407,9 @@ public class JPAXMLOverriddenAnnotationReader implements AnnotationReader { */ private void initAnnotations() { if ( annotations == null ) { - XMLContext.Default defaults = xmlContext.getDefault( className ); + // We don't want the global catalog and schema here: they are applied much later, + // when SQL gets rendered. + XMLContext.Default defaults = xmlContext.getDefaultWithoutGlobalCatalogAndSchema( className ); if ( className != null && propertyName == null ) { //is a class ManagedType managedTypeOverride = xmlContext.getManagedTypeOverride( className ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/JPAXMLOverriddenMetadataProvider.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/JPAXMLOverriddenMetadataProvider.java index e49afb8013..0bb0a99093 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/JPAXMLOverriddenMetadataProvider.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/JPAXMLOverriddenMetadataProvider.java @@ -94,7 +94,7 @@ public final class JPAXMLOverriddenMetadataProvider implements MetadataProvider else { if ( defaults == null ) { defaults = new HashMap<>(); - XMLContext.Default xmlDefaults = xmlContext.getDefault( null ); + XMLContext.Default xmlDefaults = xmlContext.getDefaultWithGlobalCatalogAndSchema(); defaults.put( "schema", xmlDefaults.getSchema() ); defaults.put( "catalog", xmlDefaults.getCatalog() ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/XMLContext.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/XMLContext.java index 5813a41369..3eaf5149df 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/XMLContext.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/internal/XMLContext.java @@ -124,12 +124,12 @@ public class XMLContext implements Serializable { managedTypeOverride.put( className, element ); Default mergedDefaults = new Default(); // Apply entity mapping defaults - mergedDefaults.override( defaults ); + mergedDefaults.overrideWithCatalogAndSchema( defaults ); // ... then apply entity settings Default fileDefaults = new Default(); fileDefaults.setMetadataComplete( element.isMetadataComplete() ); fileDefaults.setAccess( element.getAccess() ); - mergedDefaults.override( fileDefaults ); + mergedDefaults.overrideWithCatalogAndSchema( fileDefaults ); // ... and we get the merged defaults for that entity defaultsOverride.put( className, mergedDefaults ); @@ -196,16 +196,22 @@ public class XMLContext implements Serializable { return buildSafeClassName( className, defaults.getPackageName() ); } - public Default getDefault(String className) { + public Default getDefaultWithoutGlobalCatalogAndSchema(String className) { Default xmlDefault = new Default(); - xmlDefault.override( globalDefaults ); + xmlDefault.overrideWithoutCatalogAndSchema( globalDefaults ); if ( className != null ) { Default entityMappingOverriding = defaultsOverride.get( className ); - xmlDefault.override( entityMappingOverriding ); + xmlDefault.overrideWithCatalogAndSchema( entityMappingOverriding ); } return xmlDefault; } + public Default getDefaultWithGlobalCatalogAndSchema() { + Default xmlDefault = new Default(); + xmlDefault.overrideWithCatalogAndSchema( globalDefaults ); + return xmlDefault; + } + public ManagedType getManagedTypeOverride(String className) { return managedTypeOverride.get( className ); } @@ -292,7 +298,19 @@ public class XMLContext implements Serializable { this.cascadePersist = cascadePersist; } - public void override(Default globalDefault) { + public void overrideWithCatalogAndSchema(Default override) { + overrideWithoutCatalogAndSchema( override ); + if ( override != null ) { + if ( override.getSchema() != null ) { + schema = override.getSchema(); + } + if ( override.getCatalog() != null ) { + catalog = override.getCatalog(); + } + } + } + + public void overrideWithoutCatalogAndSchema(Default globalDefault) { if ( globalDefault != null ) { if ( globalDefault.getAccess() != null ) { access = globalDefault.getAccess(); @@ -300,12 +318,6 @@ public class XMLContext implements Serializable { if ( globalDefault.getPackageName() != null ) { packageName = globalDefault.getPackageName(); } - if ( globalDefault.getSchema() != null ) { - schema = globalDefault.getSchema(); - } - if ( globalDefault.getCatalog() != null ) { - catalog = globalDefault.getCatalog(); - } if ( globalDefault.getDelimitedIdentifier() != null ) { delimitedIdentifier = globalDefault.getDelimitedIdentifier(); } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/DatabaseInformationImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/DatabaseInformationImpl.java index 38748838b1..f2ad3077ad 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/DatabaseInformationImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/DatabaseInformationImpl.java @@ -32,6 +32,7 @@ import org.hibernate.tool.schema.spi.SchemaManagementTool; public class DatabaseInformationImpl implements DatabaseInformation, ExtractionContext.DatabaseObjectAccess { private final JdbcEnvironment jdbcEnvironment; + private final SqlStringGenerationContext sqlStringGenerationContext; private final ExtractionContext extractionContext; private final InformationExtractor extractor; @@ -44,6 +45,7 @@ public class DatabaseInformationImpl DdlTransactionIsolator ddlTransactionIsolator, SchemaManagementTool tool) throws SQLException { this.jdbcEnvironment = jdbcEnvironment; + this.sqlStringGenerationContext = sqlStringGenerationContext; this.extractionContext = tool.getExtractionTool().createExtractionContext( serviceRegistry, jdbcEnvironment, @@ -78,12 +80,13 @@ public class DatabaseInformationImpl @Override public boolean catalogExists(Identifier catalog) { - return extractor.catalogExists( catalog ); + return extractor.catalogExists( sqlStringGenerationContext.catalogWithDefault( catalog ) ); } @Override public boolean schemaExists(Namespace.Name namespace) { - return extractor.schemaExists( namespace.getCatalog(), namespace.getSchema() ); + return extractor.schemaExists( sqlStringGenerationContext.catalogWithDefault( namespace.getCatalog() ), + sqlStringGenerationContext.schemaWithDefault( namespace.getSchema() ) ); } @Override @@ -108,15 +111,16 @@ public class DatabaseInformationImpl } return extractor.getTable( - tableName.getCatalogName(), - tableName.getSchemaName(), + sqlStringGenerationContext.catalogWithDefault( tableName.getCatalogName() ), + sqlStringGenerationContext.schemaWithDefault( tableName.getSchemaName() ), tableName.getTableName() ); } @Override public NameSpaceTablesInformation getTablesInformation(Namespace namespace) { - return extractor.getTables( namespace.getPhysicalName().getCatalog(), namespace.getPhysicalName().getSchema() ); + return extractor.getTables( sqlStringGenerationContext.catalogWithDefault( namespace.getPhysicalName().getCatalog() ), + sqlStringGenerationContext.schemaWithDefault( namespace.getPhysicalName().getSchema() ) ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java index 761ebecc65..01e9bcbc04 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java @@ -260,7 +260,8 @@ public class SchemaCreatorImpl implements SchemaCreator { if ( tryToCreateCatalogs ) { final Identifier catalogLogicalName = namespace.getName().getCatalog(); - final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog(); + final Identifier catalogPhysicalName = + sqlStringGenerationContext.catalogWithDefault( namespace.getPhysicalName().getCatalog() ); if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) { applySqlStrings( @@ -273,9 +274,11 @@ public class SchemaCreatorImpl implements SchemaCreator { } } - if ( tryToCreateSchemas && namespace.getPhysicalName().getSchema() != null ) { + final Identifier schemaPhysicalName = + sqlStringGenerationContext.schemaWithDefault( namespace.getPhysicalName().getSchema() ); + if ( tryToCreateSchemas && schemaPhysicalName != null ) { applySqlStrings( - dialect.getCreateSchemaCommand( namespace.getPhysicalName().getSchema().render( dialect ) ), + dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ), formatter, options, targets diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/sequences/HibernateSequenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/sequences/HibernateSequenceTest.java index 30be5d21e3..9d23c00b40 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/sequences/HibernateSequenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/sequences/HibernateSequenceTest.java @@ -7,6 +7,7 @@ package org.hibernate.orm.test.annotations.id.sequences; +import org.hibernate.boot.model.relational.SqlStringGenerationContext; import org.hibernate.dialect.H2Dialect; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.enhanced.SequenceStyleGenerator; @@ -45,9 +46,10 @@ public class HibernateSequenceTest { IdentifierGenerator generator = persister.getIdentifierGenerator(); assertTrue( SequenceStyleGenerator.class.isInstance( generator ) ); SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator; + SqlStringGenerationContext sqlStringGenerationContext = scope.getSessionFactory().getSqlStringGenerationContext(); assertEquals( Table.qualify( null, SCHEMA_NAME, "HibernateSequenceEntity_SEQ" ), - seqGenerator.getDatabaseStructure().getPhysicalName().render() + sqlStringGenerationContext.format( seqGenerator.getDatabaseStructure().getPhysicalName() ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/reflection/JPAXMLOverriddenAnnotationReaderTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/reflection/JPAXMLOverriddenAnnotationReaderTest.java index ec4a2b627c..92ef4a58a1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/reflection/JPAXMLOverriddenAnnotationReaderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/reflection/JPAXMLOverriddenAnnotationReaderTest.java @@ -64,7 +64,9 @@ public class JPAXMLOverriddenAnnotationReaderTest extends BaseUnitTestCase { ); assertNotNull( reader.getAnnotation( Table.class ) ); assertEquals( "@Table not overridden", "tbl_admin", reader.getAnnotation( Table.class ).name() ); - assertEquals( "Default schema not overridden", "myschema", reader.getAnnotation( Table.class ).schema() ); + // The default schema is assigned later, when we generate SQL. + // See DefaultCatalogAndSchemaTest. + assertEquals( "Default schema overridden too soon", "", reader.getAnnotation( Table.class ).schema() ); assertEquals( "Proper @Table.uniqueConstraints", 2, reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames().length @@ -88,7 +90,9 @@ public class JPAXMLOverriddenAnnotationReaderTest extends BaseUnitTestCase { assertEquals( "default fails", 50, reader.getAnnotation( SequenceGenerator.class ).allocationSize() ); assertNotNull( "TableOverriding not working", reader.getAnnotation( TableGenerator.class ) ); assertEquals( "wrong tble name", "tablehilo", reader.getAnnotation( TableGenerator.class ).table() ); - assertEquals( "no schema overriding", "myschema", reader.getAnnotation( TableGenerator.class ).schema() ); + // The default schema is assigned later, when we generate SQL. + // See DefaultCatalogAndSchemaTest. + assertEquals( "Default schema overridden too soon", "", reader.getAnnotation( TableGenerator.class ).schema() ); reader = new JPAXMLOverriddenAnnotationReader( Match.class, context, bootstrapContext ); assertNotNull( reader.getAnnotation( Table.class ) ); @@ -98,10 +102,14 @@ public class JPAXMLOverriddenAnnotationReaderTest extends BaseUnitTestCase { assertEquals( "Java annotation not taken into account", "matchschema", reader.getAnnotation( Table.class ).schema() ); - assertEquals( "Overriding not taken into account", "mycatalog", reader.getAnnotation( Table.class ).catalog() ); + // The default schema is assigned later, when we generate SQL. + // See DefaultCatalogAndSchemaTest. + assertEquals( "Default catalog overridden too soon", "", reader.getAnnotation( Table.class ).catalog() ); assertNotNull( "SecondaryTable swallowed", reader.getAnnotation( SecondaryTables.class ) ); + // The default schema is assigned later, when we generate SQL. + // See DefaultCatalogAndSchemaTest. assertEquals( - "Default schema not taken into account", "myschema", + "Default schema not taken into account", "", reader.getAnnotation( SecondaryTables.class ).value()[0].schema() ); assertNotNull( reader.getAnnotation( Inheritance.class ) ); @@ -194,15 +202,14 @@ public class JPAXMLOverriddenAnnotationReaderTest extends BaseUnitTestCase { assertEquals( "Metadata complete should ignore java annotations", "", reader.getAnnotation( Entity.class ).name() ); - assertNotNull( reader.getAnnotation( Table.class ) ); - assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() ); - assertEquals( "Default schema not overriden", "myschema", reader.getAnnotation( Table.class ).schema() ); + // The default schema is assigned later, when we generate SQL. + // See DefaultCatalogAndSchemaTest. + assertNull( "Default schema overridden too soon", reader.getAnnotation( Table.class ) ); reader = new JPAXMLOverriddenAnnotationReader( Match.class, context, bootstrapContext ); - assertNotNull( reader.getAnnotation( Table.class ) ); - assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() ); - assertEquals( "Overriding not taken into account", "myschema", reader.getAnnotation( Table.class ).schema() ); - assertEquals( "Overriding not taken into account", "mycatalog", reader.getAnnotation( Table.class ).catalog() ); + // The default schema is assigned later, when we generate SQL. + // See DefaultCatalogAndSchemaTest. + assertNull( "Default schema overridden too soon", reader.getAnnotation( Table.class ) ); assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTable.class ) ); assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTables.class ) ); assertNull( "Ignore Java annotation", reader.getAnnotation( Inheritance.class ) );