clean up two more enums

This commit is contained in:
Gavin King 2024-09-10 07:24:10 +02:00
parent d0d6f08243
commit 2e4c2ff565
4 changed files with 67 additions and 103 deletions

View File

@ -7,7 +7,6 @@
package org.hibernate.cfg; package org.hibernate.cfg;
import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.spi.QueryPlan; import org.hibernate.query.spi.QueryPlan;
import jakarta.persistence.criteria.CriteriaDelete; import jakarta.persistence.criteria.CriteriaDelete;
@ -98,13 +97,14 @@ public interface QuerySettings {
String CRITERIA_VALUE_HANDLING_MODE = "hibernate.criteria.value_handling_mode"; String CRITERIA_VALUE_HANDLING_MODE = "hibernate.criteria.value_handling_mode";
/** /**
* Specifies the default {@linkplain NullPrecedence precedence of null values} in the * Specifies the default {@linkplain jakarta.persistence.criteria.Nulls precedence
* HQL {@code ORDER BY} clause, either {@code none}, {@code first}, or {@code last}, * of null values} sorted via the HQL {@code ORDER BY} clause, either {@code none},
* or an instance of {@link NullPrecedence}. * {@code first}, or {@code last}, or an instance of the enumeration
* {@link jakarta.persistence.criteria.Nulls}.
* <p> * <p>
* The default is {@code none}. * The default is {@code none}.
* *
* @see NullPrecedence * @see jakarta.persistence.criteria.Nulls
* @see org.hibernate.boot.SessionFactoryBuilder#applyDefaultNullPrecedence(jakarta.persistence.criteria.Nulls) * @see org.hibernate.boot.SessionFactoryBuilder#applyDefaultNullPrecedence(jakarta.persistence.criteria.Nulls)
*/ */
String DEFAULT_NULL_ORDERING = "hibernate.order_by.default_null_ordering"; String DEFAULT_NULL_ORDERING = "hibernate.order_by.default_null_ordering";
@ -245,7 +245,7 @@ public interface QuerySettings {
/** /**
* For database supporting name parameters this setting allows to use named parameter is the procedure call. * For database supporting name parameters this setting allows to use named parameter is the procedure call.
* * <p>
* By default, this is set to false * By default, this is set to false
*/ */
String QUERY_PASS_PROCEDURE_PARAMETER_NAMES = "hibernate.query.pass_procedure_paramater_names"; String QUERY_PASS_PROCEDURE_PARAMETER_NAMES = "hibernate.query.pass_procedure_paramater_names";

View File

@ -7,16 +7,16 @@
package org.hibernate.query; package org.hibernate.query;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.cfg.AvailableSettings;
/** /**
* This enum defines how {@link org.hibernate.annotations.Immutable} entities are handled when * Controls for how {@linkplain org.hibernate.annotations.Immutable immutable} entities
* executing a bulk update statement. * are handled when executing a bulk update statement.
* <ul> * <ul>
* <li>By default, the {@link ImmutableEntityUpdateQueryHandlingMode#WARNING} mode is used, * <li>By default, the {@link #WARNING} mode is used, and a warning log message is issued
* and a warning log message is issued when an {@link org.hibernate.annotations.Immutable} * when an immutable entity is updated via a bulk update statement.
* entity is to be updated via a bulk update statement. * <li>If the {@link #EXCEPTION} mode is configured, then a {@link HibernateException} is
* <li>If the {@link ImmutableEntityUpdateQueryHandlingMode#EXCEPTION} mode is used, then a * thrown instead.
* {@link HibernateException} is thrown instead.
* </ul> * </ul>
* *
* @see org.hibernate.cfg.AvailableSettings#IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE * @see org.hibernate.cfg.AvailableSettings#IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE
@ -29,31 +29,32 @@ public enum ImmutableEntityUpdateQueryHandlingMode {
EXCEPTION; EXCEPTION;
/** /**
* Interpret the configured {@link ImmutableEntityUpdateQueryHandlingMode} value. * Interpret the setting specified via
* Valid values are either a {@link ImmutableEntityUpdateQueryHandlingMode} object or * {@value AvailableSettings#IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE}.
* its string representation. For string values, the matching is case-insensitive, * <p>
* so you can use either {@code warning} or {@code exception}. * Valid values are an instance of {@link ImmutableEntityUpdateQueryHandlingMode}
* or its string representation. For string values, the matching is case-insensitive,
* so {@code warning} or {@code exception} are legal values.
* *
* @param mode configured {@link ImmutableEntityUpdateQueryHandlingMode} representation * @param setting the configuration setting.
* @return associated {@link ImmutableEntityUpdateQueryHandlingMode} object * @return the associated {@link ImmutableEntityUpdateQueryHandlingMode} object
*/ */
public static ImmutableEntityUpdateQueryHandlingMode interpret(Object mode) { public static ImmutableEntityUpdateQueryHandlingMode interpret(Object setting) {
if ( mode == null ) { if ( setting == null ) {
return WARNING; return WARNING;
} }
else if ( mode instanceof ImmutableEntityUpdateQueryHandlingMode ) { else if ( setting instanceof ImmutableEntityUpdateQueryHandlingMode mode ) {
return (ImmutableEntityUpdateQueryHandlingMode) mode; return mode;
} }
else if ( mode instanceof String ) { else if ( setting instanceof String string ) {
for ( ImmutableEntityUpdateQueryHandlingMode value : values() ) { for ( ImmutableEntityUpdateQueryHandlingMode value : values() ) {
if ( value.name().equalsIgnoreCase( (String) mode ) ) { if ( value.name().equalsIgnoreCase( string ) ) {
return value; return value;
} }
} }
} }
throw new HibernateException( throw new HibernateException( "Unrecognized value '" + setting
"Unrecognized immutable_entity_update_query_handling_mode value : " + mode + "' specified via '" + AvailableSettings.IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE
+ ". Supported values include 'warning' and 'exception'." + "' (should be 'warning' or 'exception')" );
);
} }
} }

View File

@ -6,7 +6,6 @@
*/ */
package org.hibernate.query; package org.hibernate.query;
import org.hibernate.AssertionFailure;
import org.hibernate.dialect.NullOrdering; import org.hibernate.dialect.NullOrdering;
import jakarta.persistence.criteria.Nulls; import jakarta.persistence.criteria.Nulls;
@ -24,21 +23,15 @@ public enum NullPrecedence {
/** /**
* Null precedence not specified. Relies on the RDBMS implementation. * Null precedence not specified. Relies on the RDBMS implementation.
*/ */
NONE( Nulls.NONE ), NONE,
/** /**
* Null values appear at the beginning of the sorted collection. * Null values appear at the beginning of the sorted collection.
*/ */
FIRST( Nulls.FIRST ), FIRST,
/** /**
* Null values appear at the end of the sorted collection. * Null values appear at the end of the sorted collection.
*/ */
LAST( Nulls.LAST ); LAST;
private final Nulls jpaValue;
NullPrecedence(Nulls jpaValue) {
this.jpaValue = jpaValue;
}
/** /**
* Is this null precedence the default for the given sort order and null ordering. * Is this null precedence the default for the given sort order and null ordering.
@ -47,38 +40,21 @@ public enum NullPrecedence {
*/ */
@Deprecated(since = "7.0", forRemoval = true) @Deprecated(since = "7.0", forRemoval = true)
public boolean isDefaultOrdering(SortDirection sortOrder, NullOrdering nullOrdering) { public boolean isDefaultOrdering(SortDirection sortOrder, NullOrdering nullOrdering) {
switch (this) { return switch (this) {
case NONE: case NONE -> true;
return true; case FIRST -> switch (nullOrdering) {
case FIRST: case FIRST -> true;
switch ( nullOrdering ) { case LAST -> false;
case FIRST: case SMALLEST -> sortOrder == SortDirection.ASCENDING;
return true; case GREATEST -> sortOrder == SortDirection.DESCENDING;
case LAST: };
return false; case LAST -> switch (nullOrdering) {
case SMALLEST: case LAST -> true;
return sortOrder == SortDirection.ASCENDING; case FIRST -> false;
case GREATEST: case SMALLEST -> sortOrder == SortDirection.DESCENDING;
return sortOrder == SortDirection.DESCENDING; case GREATEST -> sortOrder == SortDirection.ASCENDING;
default: };
throw new AssertionFailure("Unrecognized NullOrdering"); };
}
case LAST:
switch ( nullOrdering ) {
case LAST:
return true;
case FIRST:
return false;
case SMALLEST:
return sortOrder == SortDirection.DESCENDING;
case GREATEST:
return sortOrder == SortDirection.ASCENDING;
default:
throw new AssertionFailure("Unrecognized NullOrdering");
}
default:
throw new AssertionFailure("Unrecognized NullPrecedence");
}
} }
/** /**
@ -112,22 +88,19 @@ public enum NullPrecedence {
} }
public Nulls getJpaValue() { public Nulls getJpaValue() {
return jpaValue; return switch (this) {
case NONE -> Nulls.NONE;
case FIRST -> Nulls.FIRST;
case LAST -> Nulls.LAST;
};
} }
public static NullPrecedence fromJpaValue(Nulls jpaValue) { public static NullPrecedence fromJpaValue(Nulls jpaValue) {
switch ( jpaValue ) { return switch (jpaValue) {
case NONE: { case NONE -> NullPrecedence.NONE;
return NullPrecedence.NONE; case FIRST -> NullPrecedence.FIRST;
} case LAST -> NullPrecedence.LAST;
case FIRST: { };
return NullPrecedence.FIRST;
}
case LAST: {
return NullPrecedence.LAST;
}
}
throw new IllegalArgumentException( "Unexpected JPA Nulls - " + jpaValue );
} }
} }

View File

@ -20,30 +20,20 @@ public enum SortDirection {
DESCENDING; DESCENDING;
public SortDirection reverse() { public SortDirection reverse() {
switch (this) { return switch (this) {
case ASCENDING: case ASCENDING -> DESCENDING;
return DESCENDING; case DESCENDING -> ASCENDING;
case DESCENDING: };
return ASCENDING;
default:
return this;
}
} }
public static SortDirection interpret(String value) { public static SortDirection interpret(String value) {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
else return switch ( value.toLowerCase(Locale.ROOT) ) {
switch ( value.toLowerCase(Locale.ROOT) ) { case "asc", "ascending" -> ASCENDING;
case "asc": case "desc", "descending" -> DESCENDING;
case "ascending": default -> throw new IllegalArgumentException( "Unknown sort order: " + value );
return ASCENDING; };
case "desc":
case "descending":
return DESCENDING;
default:
throw new IllegalArgumentException( "Unknown sort order: " + value );
}
} }
} }