Skip to content

Commit

Permalink
Expression#as() broke since Hibernate 6.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Jan 24, 2025
1 parent bf62160 commit af07224
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/org/omnifaces/persistence/JPA.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static java.util.stream.Collectors.toList;
import static org.omnifaces.persistence.Database.POSTGRESQL;
import static org.omnifaces.persistence.Provider.HIBERNATE;
import static org.omnifaces.utils.reflect.Reflections.findClass;
import static org.omnifaces.utils.reflect.Reflections.invokeMethod;
import static org.omnifaces.utils.stream.Collectors.toMap;
import static org.omnifaces.utils.stream.Streams.stream;

Expand Down Expand Up @@ -77,6 +79,9 @@ public final class JPA {
public static final String QUERY_HINT_CACHE_RETRIEVE_MODE = "jakarta.persistence.cache.retrieveMode"; // USE | BYPASS
public static final String PROPERTY_VALIDATION_MODE = "jakarta.persistence.validation.mode"; // AUTO | CALLBACK | NONE

// Private constants ------------------------------------------------------------------------------------------------------------------

private static final Optional<Class<Object>> HIBERNATE_6_6_0_JPA_EXPRESSION = findClass("org.hibernate.query.criteria.JpaExpression");

// Constructors -----------------------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -353,8 +358,16 @@ public static Expression<String> concat(CriteriaBuilder builder, Object... expre
*/
@SuppressWarnings("unchecked")
public static Expression<String> castAsString(CriteriaBuilder builder, Expression<?> expression) {

// NOTE: Improvement for all providers is expected in JPA 3.2 with new Expression#cast() API.

if (Provider.is(HIBERNATE)) {
return expression.as(String.class);
if (HIBERNATE_6_6_0_JPA_EXPRESSION.isPresent() && HIBERNATE_6_6_0_JPA_EXPRESSION.get().isInstance(expression)) {
return invokeMethod(expression, "cast", String.class); // https://hibernate.atlassian.net/browse/HHH-18710
}
else {
return expression.as(String.class);
}
}

// EclipseLink and OpenJPA have a broken Expression#as() implementation, need to delegate to DB specific function.
Expand Down

0 comments on commit af07224

Please sign in to comment.