From e422d913dbb5d1e7f17cb8706c9da1e07f25e4e5 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Fri, 24 Sep 2021 12:43:26 -0500 Subject: [PATCH] HHH-14837 - Move to Jakarta EE improved support for schema tooling settings --- .../internal/util/NullnessHelper.java | 29 ++++- .../HibernateSchemaManagementTool.java | 113 ++++++++++++------ .../jakarta/JakartaSchemaToolingTests.java | 6 +- 3 files changed, 102 insertions(+), 46 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/NullnessHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/NullnessHelper.java index 8bad88850f..acdc8fe3a3 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/NullnessHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/NullnessHelper.java @@ -6,6 +6,7 @@ */ package org.hibernate.internal.util; +import java.util.function.Function; import java.util.function.Supplier; /** @@ -66,20 +67,36 @@ public class NullnessHelper { */ @SafeVarargs public static T coalesceSuppliedValues(Supplier... valueSuppliers) { + return coalesceSuppliedValues( + (value) -> ( value instanceof String && StringHelper.isNotEmpty( (String) value ) ) + || value != null, + valueSuppliers + ); + } + + /** + * Operates like SQL coalesce expression, returning the first non-empty value + * + * @implNote This impl treats empty strings (`""`) as null. + * + * @param valueSuppliers List of value Suppliers + * @param Generic type of values to coalesce + * + * @return The first non-empty value, or null if all values were empty + */ + @SafeVarargs + public static T coalesceSuppliedValues(Function checker, Supplier... valueSuppliers) { if ( valueSuppliers == null ) { return null; } + for ( Supplier valueSupplier : valueSuppliers ) { if ( valueSupplier != null ) { final T value = valueSupplier.get(); - if ( value instanceof String ) { - if ( StringHelper.isNotEmpty( (String) value ) ) { - return value; - } - } - else if ( value != null ) { + if ( checker.apply( value ) ) { return value; } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/HibernateSchemaManagementTool.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/HibernateSchemaManagementTool.java index f9dc8bc83d..ddfcda89ca 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/HibernateSchemaManagementTool.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/HibernateSchemaManagementTool.java @@ -51,9 +51,19 @@ import org.hibernate.tool.schema.spi.TargetDescriptor; import org.jboss.logging.Logger; +import static org.hibernate.cfg.AvailableSettings.DIALECT_DB_NAME; +import static org.hibernate.cfg.AvailableSettings.DIALECT_DB_VERSION; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_CONNECTION; +import static org.hibernate.cfg.AvailableSettings.HBM2DDL_DB_MAJOR_VERSION; +import static org.hibernate.cfg.AvailableSettings.HBM2DDL_DB_MINOR_VERSION; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_DELIMITER; +import static org.hibernate.cfg.AvailableSettings.JAKARTA_DIALECT_DB_VERSION; import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_CONNECTION; +import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_DB_MAJOR_VERSION; +import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_DB_MINOR_VERSION; +import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_DB_NAME; +import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER; +import static org.hibernate.internal.util.NullnessHelper.coalesceSuppliedValues; /** * The standard Hibernate implementation for performing schema management. @@ -208,66 +218,91 @@ public class HibernateSchemaManagementTool implements SchemaManagementTool, Serv final JdbcContextBuilder jdbcContextBuilder = new JdbcContextBuilder( serviceRegistry ); // see if a specific connection has been provided - final Connection providedConnection = (Connection) configurationValues.get( HBM2DDL_CONNECTION ); + final Connection providedConnection = (Connection) coalesceSuppliedValues( + () -> configurationValues.get( JAKARTA_HBM2DDL_CONNECTION ), + () -> { + final Object value = configurationValues.get( HBM2DDL_CONNECTION ); + if ( value != null ) { + DEPRECATION_LOGGER.deprecatedSetting( HBM2DDL_CONNECTION, JAKARTA_HBM2DDL_CONNECTION ); + } + return value; + } + ); if ( providedConnection != null ) { jdbcContextBuilder.jdbcConnectionAccess = new JdbcConnectionAccessProvidedConnectionImpl( providedConnection ); } - else { - final Connection jakartaProvidedConnection = (Connection) configurationValues.get( JAKARTA_HBM2DDL_CONNECTION ); - if ( jakartaProvidedConnection != null ) { - jdbcContextBuilder.jdbcConnectionAccess = new JdbcConnectionAccessProvidedConnectionImpl( jakartaProvidedConnection ); - } - } - // see if a specific Dialect override has been provided... - String dbName = (String) configurationValues.get( AvailableSettings.DIALECT_DB_NAME ); - if ( dbName == null ) { - dbName = (String) configurationValues.get( AvailableSettings.JAKARTA_HBM2DDL_DB_NAME ); - } - final String explicitDbName = dbName; - if ( StringHelper.isNotEmpty( explicitDbName ) ) { - String dbVersion = (String) configurationValues.get( AvailableSettings.DIALECT_DB_VERSION ); - if ( dbVersion == null ) { - dbVersion = (String) configurationValues.get( AvailableSettings.JAKARTA_DIALECT_DB_VERSION ); - } - String dbMajor = (String) configurationValues.get( AvailableSettings.DIALECT_DB_MAJOR_VERSION ); - if ( dbMajor == null ) { - dbMajor = (String) configurationValues.get( AvailableSettings.JAKARTA_HBM2DDL_DB_MAJOR_VERSION ); - } - String dbMinor = (String) configurationValues.get( AvailableSettings.DIALECT_DB_MINOR_VERSION ); - if ( dbMinor == null ) { - dbMinor = (String) configurationValues.get( AvailableSettings.JAKARTA_HBM2DDL_DB_MINOR_VERSION ); - } - final String explicitDbVersion = dbVersion; - final String explicitDbMajor = dbMajor; - final String explicitDbMinor = dbMinor; + // see if a specific Dialect override has been provided through database name, version, etc + final String dbName = (String) coalesceSuppliedValues( + () -> configurationValues.get( JAKARTA_HBM2DDL_DB_NAME ), + () -> { + final String name = (String) configurationValues.get( DIALECT_DB_NAME ); + if ( StringHelper.isNotEmpty( name ) ) { + DEPRECATION_LOGGER.deprecatedSetting( DIALECT_DB_NAME, JAKARTA_HBM2DDL_DB_NAME ); + } + return name; + } + ); + if ( dbName != null ) { + final String dbVersion = (String) coalesceSuppliedValues( + () -> configurationValues.get( JAKARTA_DIALECT_DB_VERSION ), + () -> { + final String name = (String) configurationValues.get( DIALECT_DB_VERSION ); + if ( StringHelper.isNotEmpty( name ) ) { + DEPRECATION_LOGGER.deprecatedSetting( DIALECT_DB_VERSION, JAKARTA_DIALECT_DB_VERSION ); + } + return name; + } + ); + + final String dbMajor = (String) coalesceSuppliedValues( + () -> configurationValues.get( JAKARTA_HBM2DDL_DB_MAJOR_VERSION ), + () -> { + final String name = (String) configurationValues.get( HBM2DDL_DB_MAJOR_VERSION ); + if ( StringHelper.isNotEmpty( name ) ) { + DEPRECATION_LOGGER.deprecatedSetting( HBM2DDL_DB_MAJOR_VERSION, JAKARTA_HBM2DDL_DB_MAJOR_VERSION ); + } + return name; + } + ); + + final String dbMinor = (String) coalesceSuppliedValues( + () -> configurationValues.get( JAKARTA_HBM2DDL_DB_MINOR_VERSION ), + () -> { + final String name = (String) configurationValues.get( HBM2DDL_DB_MINOR_VERSION ); + if ( StringHelper.isNotEmpty( name ) ) { + DEPRECATION_LOGGER.deprecatedSetting( HBM2DDL_DB_MINOR_VERSION, JAKARTA_HBM2DDL_DB_MINOR_VERSION ); + } + return name; + } + ); final Dialect indicatedDialect = serviceRegistry.getService( DialectResolver.class ).resolveDialect( new DialectResolutionInfo() { @Override public String getDatabaseName() { - return explicitDbName; + return dbName; } @Override public String getDatabaseVersion() { - return explicitDbVersion == null + return dbVersion == null ? String.valueOf( NO_VERSION ) : - explicitDbVersion; + dbVersion; } @Override public int getDatabaseMajorVersion() { - return StringHelper.isEmpty( explicitDbMajor ) + return StringHelper.isEmpty( dbMajor ) ? NO_VERSION - : Integer.parseInt( explicitDbMajor ); + : Integer.parseInt( dbMajor ); } @Override public int getDatabaseMinorVersion() { - return StringHelper.isEmpty( explicitDbMinor ) + return StringHelper.isEmpty( dbMinor ) ? NO_VERSION - : Integer.parseInt( explicitDbMinor ); + : Integer.parseInt( dbMinor ); } @Override @@ -290,9 +325,9 @@ public class HibernateSchemaManagementTool implements SchemaManagementTool, Serv if ( indicatedDialect == null ) { log.debugf( "Unable to resolve indicated Dialect resolution info (%s, %s, %s)", - explicitDbName, - explicitDbMajor, - explicitDbMinor + dbName, + dbMajor, + dbMinor ); } else { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/jakarta/JakartaSchemaToolingTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/jakarta/JakartaSchemaToolingTests.java index 49b5d82397..4690a25d0c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/jakarta/JakartaSchemaToolingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/jakarta/JakartaSchemaToolingTests.java @@ -90,9 +90,13 @@ public class JakartaSchemaToolingTests { JAKARTA_HBM2DDL_DATABASE_ACTION, Action.CREATE_DROP, HBM2DDL_DATABASE_ACTION, Action.NONE, JAKARTA_JPA_JDBC_DRIVER, Environment.getProperties().get( AvailableSettings.DRIVER ), + JPA_JDBC_DRIVER, "does.not.exist", JAKARTA_JPA_JDBC_URL, Environment.getProperties().get( AvailableSettings.URL ), + JPA_JDBC_URL, "jdbc:na:nowhere", JAKARTA_JPA_JDBC_USER, Environment.getProperties().get( AvailableSettings.USER ), - JAKARTA_JPA_JDBC_PASSWORD, Environment.getProperties().get( AvailableSettings.PASS ) + JPA_JDBC_USER, "goofy", + JAKARTA_JPA_JDBC_PASSWORD, Environment.getProperties().get( AvailableSettings.PASS ), + JPA_JDBC_PASSWORD, "goober" ) ) { tryQuery( sessionFactory ); }