HHH-17746 add typed setProperty() methods to Configuration

and fix handling of DEFAULT_NULL_ORDERING
This commit is contained in:
Gavin King 2024-02-17 10:18:20 +01:00
parent 3769d4c233
commit e732cddb09
7 changed files with 86 additions and 42 deletions

View File

@ -221,9 +221,9 @@ SessionFactory sessionFactory =
.setProperty(AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION,
Action.SPEC_ACTION_DROP_AND_CREATE)
// SQL statement logging
.setProperty(AvailableSettings.SHOW_SQL, TRUE.toString())
.setProperty(AvailableSettings.FORMAT_SQL, TRUE.toString())
.setProperty(AvailableSettings.HIGHLIGHT_SQL, TRUE.toString())
.setProperty(AvailableSettings.SHOW_SQL, true)
.setProperty(AvailableSettings.FORMAT_SQL, true)
.setProperty(AvailableSettings.HIGHLIGHT_SQL, true)
// Create a new SessionFactory
.buildSessionFactory();
----

View File

@ -254,11 +254,11 @@ public class Main {
.setProperty(USER, "sa")
.setProperty(PASS, "")
// use Agroal connection pool
.setProperty("hibernate.agroal.maxSize", "20")
.setProperty("hibernate.agroal.maxSize", 20)
// display SQL in console
.setProperty(SHOW_SQL, TRUE.toString())
.setProperty(FORMAT_SQL, TRUE.toString())
.setProperty(HIGHLIGHT_SQL, TRUE.toString())
.setProperty(SHOW_SQL, true)
.setProperty(FORMAT_SQL, true)
.setProperty(HIGHLIGHT_SQL, true)
.buildSessionFactory();
// export the inferred database schema

View File

@ -129,6 +129,7 @@ import static org.hibernate.cfg.AvailableSettings.USE_SQL_COMMENTS;
import static org.hibernate.cfg.AvailableSettings.USE_STRUCTURED_CACHE;
import static org.hibernate.cfg.AvailableSettings.USE_SUBSELECT_FETCH;
import static org.hibernate.cfg.CacheSettings.QUERY_CACHE_LAYOUT;
import static org.hibernate.cfg.QuerySettings.DEFAULT_NULL_ORDERING;
import static org.hibernate.cfg.QuerySettings.PORTABLE_INTEGER_DIVISION;
import static org.hibernate.engine.config.spi.StandardConverters.BOOLEAN;
import static org.hibernate.internal.CoreLogging.messageLogger;
@ -377,10 +378,19 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
this.defaultBatchFetchSize = getInt( DEFAULT_BATCH_FETCH_SIZE, configurationSettings, -1 );
this.subselectFetchEnabled = getBoolean( USE_SUBSELECT_FETCH, configurationSettings );
this.maximumFetchDepth = getInteger( MAX_FETCH_DEPTH, configurationSettings );
final String defaultNullPrecedence = getString(
AvailableSettings.DEFAULT_NULL_ORDERING, configurationSettings, "none", "first", "last"
);
this.defaultNullPrecedence = NullPrecedence.parse( defaultNullPrecedence );
final Object defaultNullPrecedence = configurationSettings.get( DEFAULT_NULL_ORDERING );
if ( defaultNullPrecedence instanceof NullPrecedence ) {
this.defaultNullPrecedence = (NullPrecedence) defaultNullPrecedence;
}
else if ( defaultNullPrecedence instanceof String ) {
this.defaultNullPrecedence = NullPrecedence.parse( (String) defaultNullPrecedence );
}
else if ( defaultNullPrecedence != null ) {
throw new IllegalArgumentException( "Configuration property " + DEFAULT_NULL_ORDERING
+ " value [" + defaultNullPrecedence + "] is not supported" );
}
this.orderUpdatesEnabled = getBoolean( ORDER_UPDATES, configurationSettings );
this.orderInsertsEnabled = getBoolean( ORDER_INSERTS, configurationSettings );
@ -558,7 +568,8 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
this.jdbcTimeZone = TimeZone.getTimeZone( ZoneId.of((String) jdbcTimeZoneValue) );
}
else if ( jdbcTimeZoneValue != null ) {
throw new IllegalArgumentException( "Configuration property " + JDBC_TIME_ZONE + " value [" + jdbcTimeZoneValue + "] is not supported" );
throw new IllegalArgumentException( "Configuration property " + JDBC_TIME_ZONE
+ " value [" + jdbcTimeZoneValue + "] is not supported" );
}
this.criteriaValueHandlingMode = ValueHandlingMode.interpret(

View File

@ -283,6 +283,62 @@ public class Configuration {
return this;
}
/**
* Set a property to a boolean value by name
*
* @param propertyName The name of the property to set
* @param value The new boolean property value
*
* @return {@code this} for method chaining
*
* @since 6.5
*/
public Configuration setProperty(String propertyName, boolean value) {
return setProperty( propertyName, Boolean.toString(value) );
}
/**
* Set a property to a Java class name
*
* @param propertyName The name of the property to set
* @param value The Java class
*
* @return {@code this} for method chaining
*
* @since 6.5
*/
public Configuration setProperty(String propertyName, Class<?> value) {
return setProperty( propertyName, value.getName() );
}
/**
* Set a property to the name of a value of an enumerated type
*
* @param propertyName The name of the property to set
* @param value A value of an enumerated type
*
* @return {@code this} for method chaining
*
* @since 6.5
*/
public Configuration setProperty(String propertyName, Enum<?> value) {
return setProperty( propertyName, value.name() );
}
/**
* Set a property to an integer value by name
*
* @param propertyName The name of the property to set
* @param value The new integer property value
*
* @return {@code this} for method chaining
*
* @since 6.5
*/
public Configuration setProperty(String propertyName, int value) {
return setProperty( propertyName, Integer.toString(value) );
}
/**
* Add the given properties to ours.
*

View File

@ -98,8 +98,9 @@ public interface QuerySettings {
String CRITERIA_VALUE_HANDLING_MODE = "hibernate.criteria.value_handling_mode";
/**
* Specifies the default {@linkplain NullPrecedence precedence of null values} in the HQL
* {@code ORDER BY} clause, either {@code none}, {@code first}, or {@code last}.
* Specifies the default {@linkplain NullPrecedence precedence of null values} in the
* HQL {@code ORDER BY} clause, either {@code none}, {@code first}, or {@code last},
* or an instance of {@link NullPrecedence}.
* <p>
* The default is {@code none}.
*

View File

@ -88,30 +88,6 @@ public final class ConfigurationHelper {
return defaultValueSupplier.get();
}
/**
* Get the config value as a {@link String}.
*
* @param name The config setting name.
* @param values The map of config parameters.
* @param defaultValue The default value to use if not found.
* @param otherSupportedValues List of other supported values. Does not need to contain the default one.
*
* @return The value.
*
* @throws ConfigurationException Unsupported value provided.
*
*/
public static String getString(String name, Map values, String defaultValue, String ... otherSupportedValues) {
final String value = getString( name, values, defaultValue );
if ( !defaultValue.equals( value ) && ArrayHelper.indexOf( otherSupportedValues, value ) == -1 ) {
throw new ConfigurationException(
"Unsupported configuration [name=" + name + ", value=" + value + "]. " +
"Choose value between: '" + defaultValue + "', '" + String.join( "', '", otherSupportedValues ) + "'."
);
}
return value;
}
/**
* Get the config value as a boolean (default of false)
*

View File

@ -191,10 +191,10 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
}
}
configuration.setImplicitNamingStrategy( ImplicitNamingStrategyLegacyJpaImpl.INSTANCE );
configuration.setProperty( Environment.DIALECT, getDialect().getClass().getName() );
configuration.getProperties().put( PersistentTableStrategy.DROP_ID_TABLES, "true" );
configuration.getProperties().put( GlobalTemporaryTableMutationStrategy.DROP_ID_TABLES, "true" );
configuration.getProperties().put( LocalTemporaryTableMutationStrategy.DROP_ID_TABLES, "true" );
configuration.setProperty( Environment.DIALECT, getDialect().getClass() );
configuration.getProperties().put( PersistentTableStrategy.DROP_ID_TABLES, true );
configuration.getProperties().put( GlobalTemporaryTableMutationStrategy.DROP_ID_TABLES, true );
configuration.getProperties().put( LocalTemporaryTableMutationStrategy.DROP_ID_TABLES, true );
ServiceRegistryUtil.applySettings( configuration.getStandardServiceRegistryBuilder() );
return configuration;
}