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;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.spi.QueryPlan;
import jakarta.persistence.criteria.CriteriaDelete;
@ -98,13 +97,14 @@ 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},
* or an instance of {@link NullPrecedence}.
* Specifies the default {@linkplain jakarta.persistence.criteria.Nulls precedence
* of null values} sorted via the HQL {@code ORDER BY} clause, either {@code none},
* {@code first}, or {@code last}, or an instance of the enumeration
* {@link jakarta.persistence.criteria.Nulls}.
* <p>
* The default is {@code none}.
*
* @see NullPrecedence
* @see jakarta.persistence.criteria.Nulls
* @see org.hibernate.boot.SessionFactoryBuilder#applyDefaultNullPrecedence(jakarta.persistence.criteria.Nulls)
*/
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.
*
* <p>
* By default, this is set to false
*/
String QUERY_PASS_PROCEDURE_PARAMETER_NAMES = "hibernate.query.pass_procedure_paramater_names";

View File

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

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.query;
import org.hibernate.AssertionFailure;
import org.hibernate.dialect.NullOrdering;
import jakarta.persistence.criteria.Nulls;
@ -24,21 +23,15 @@ public enum NullPrecedence {
/**
* Null precedence not specified. Relies on the RDBMS implementation.
*/
NONE( Nulls.NONE ),
NONE,
/**
* Null values appear at the beginning of the sorted collection.
*/
FIRST( Nulls.FIRST ),
FIRST,
/**
* Null values appear at the end of the sorted collection.
*/
LAST( Nulls.LAST );
private final Nulls jpaValue;
NullPrecedence(Nulls jpaValue) {
this.jpaValue = jpaValue;
}
LAST;
/**
* 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)
public boolean isDefaultOrdering(SortDirection sortOrder, NullOrdering nullOrdering) {
switch (this) {
case NONE:
return true;
case FIRST:
switch ( nullOrdering ) {
case FIRST:
return true;
case LAST:
return false;
case SMALLEST:
return sortOrder == SortDirection.ASCENDING;
case GREATEST:
return sortOrder == SortDirection.DESCENDING;
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");
}
return switch (this) {
case NONE -> true;
case FIRST -> switch (nullOrdering) {
case FIRST -> true;
case LAST -> false;
case SMALLEST -> sortOrder == SortDirection.ASCENDING;
case GREATEST -> sortOrder == SortDirection.DESCENDING;
};
case LAST -> switch (nullOrdering) {
case LAST -> true;
case FIRST -> false;
case SMALLEST -> sortOrder == SortDirection.DESCENDING;
case GREATEST -> sortOrder == SortDirection.ASCENDING;
};
};
}
/**
@ -112,22 +88,19 @@ public enum NullPrecedence {
}
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) {
switch ( jpaValue ) {
case NONE: {
return NullPrecedence.NONE;
}
case FIRST: {
return NullPrecedence.FIRST;
}
case LAST: {
return NullPrecedence.LAST;
}
}
return switch (jpaValue) {
case NONE -> NullPrecedence.NONE;
case FIRST -> NullPrecedence.FIRST;
case LAST -> NullPrecedence.LAST;
};
throw new IllegalArgumentException( "Unexpected JPA Nulls - " + jpaValue );
}
}

View File

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