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 05091caa25..bef59f4503 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 @@ -41,8 +41,8 @@ public class SqlStringGenerationContextImpl /** * @param jdbcEnvironment The JDBC environment, to extract the dialect, identifier helper, etc. * @param database The database metadata, to retrieve the implicit namespace name configured through XML mapping. - * @param defaultCatalog The default catalog to use, unless an implicit catalog was configured through XML mapping. - * @param defaultSchema The default schema to use, unless an implicit schema was configured through XML mapping. + * @param defaultCatalog The default catalog to use; if {@code null}, will use the implicit catalog that was configured through XML mapping. + * @param defaultSchema The default schema to use; if {@code null}, will use the implicit schema that was configured through XML mapping. * @return An {@link SqlStringGenerationContext}. */ public static SqlStringGenerationContext fromExplicit(JdbcEnvironment jdbcEnvironment, @@ -52,15 +52,17 @@ public class SqlStringGenerationContextImpl NameQualifierSupport nameQualifierSupport = jdbcEnvironment.getNameQualifierSupport(); Identifier actualDefaultCatalog = null; if ( nameQualifierSupport.supportsCatalogs() ) { - actualDefaultCatalog = implicitNamespaceName.getCatalog() != null - ? implicitNamespaceName.getCatalog() - : identifierHelper.toIdentifier( defaultCatalog ); + actualDefaultCatalog = identifierHelper.toIdentifier( defaultCatalog ); + if ( actualDefaultCatalog == null ) { + actualDefaultCatalog = implicitNamespaceName.getCatalog(); + } } Identifier actualDefaultSchema = null; if ( nameQualifierSupport.supportsSchemas() ) { - actualDefaultSchema = implicitNamespaceName.getSchema() != null - ? implicitNamespaceName.getSchema() - : identifierHelper.toIdentifier( defaultSchema ); + actualDefaultSchema = identifierHelper.toIdentifier( defaultSchema ); + if ( defaultSchema == null ) { + actualDefaultSchema = implicitNamespaceName.getSchema(); + } } return new SqlStringGenerationContextImpl( jdbcEnvironment, actualDefaultCatalog, actualDefaultSchema ); } 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 1b89ae77ed..ad9c46ad38 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Settings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Settings.java @@ -61,8 +61,8 @@ public final class Settings { public Settings(SessionFactoryOptions sessionFactoryOptions, String defaultCatalogName, String defaultSchemaName) { this.sessionFactoryOptions = sessionFactoryOptions; - this.defaultCatalogName = defaultCatalogName != null ? defaultCatalogName : sessionFactoryOptions.getDefaultCatalog(); - this.defaultSchemaName = defaultSchemaName != null ? defaultSchemaName : sessionFactoryOptions.getDefaultSchema(); + this.defaultCatalogName = sessionFactoryOptions.getDefaultCatalog() != null ? sessionFactoryOptions.getDefaultCatalog() : defaultCatalogName; + this.defaultSchemaName = sessionFactoryOptions.getDefaultSchema() != null ? sessionFactoryOptions.getDefaultSchema() : defaultSchemaName; if ( LOG.isDebugEnabled() ) { LOG.debugf( "SessionFactory name : %s", sessionFactoryOptions.getSessionFactoryName() ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/boot/database/qualfiedTableNaming/DefaultCatalogAndSchemaTest.java b/hibernate-core/src/test/java/org/hibernate/test/boot/database/qualfiedTableNaming/DefaultCatalogAndSchemaTest.java index f0c497a6ef..37fc2926bb 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/boot/database/qualfiedTableNaming/DefaultCatalogAndSchemaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/boot/database/qualfiedTableNaming/DefaultCatalogAndSchemaTest.java @@ -129,9 +129,9 @@ public class DefaultCatalogAndSchemaTest { // HHH-14922: Inconsistent precedence of orm.xml implicit catalog/schema over "default_catalog"/"default_schema" params.add( new Object[] { mode, "implicit-global-catalog-and-schema.orm.xml", "someDefaultCatalog", "someDefaultSchema", - // The implicit catalog/schema defined in the mapping should take precedence - // over the default catalog/schema defined in settings. - "someImplicitCatalog", "someImplicitSchema" } ); + // The default catalog/schema should replace the + // implicit catalog/schema defined in the mapping. + "someDefaultCatalog", "someDefaultSchema" } ); } return params; }