This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 8.0.x-hibernate7-dev in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit cd5579f94b6731bc5b3753e915e3317448eded3c Author: Walter Duque de Estrada <[email protected]> AuthorDate: Sun Mar 8 12:28:34 2026 -0500 hibernate7: added cache and readOnly support to CriteriaMethodInvoker --- .../groovy/grails/orm/CriteriaMethodInvoker.java | 36 +++++++++++++--------- .../main/groovy/grails/orm/CriteriaMethods.java | 4 ++- .../grails/orm/HibernateCriteriaBuilder.java | 3 +- .../grails/orm/hibernate/cfg/IdentityEnumType.java | 1 - .../grails/orm/CriteriaMethodInvokerSpec.groovy | 16 ++++++++++ 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethodInvoker.java b/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethodInvoker.java index 6a7ac3aee5..5b28247255 100644 --- a/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethodInvoker.java +++ b/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethodInvoker.java @@ -82,7 +82,7 @@ public class CriteriaMethodInvoker { // Check for pagination params if (method == CriteriaMethods.LIST_CALL && args.length == 2) { builder.setPaginationEnabledList(true); - if (args[0] instanceof Map map) { + if (args[0] instanceof Map<?, ?> map) { if (map.get("max") instanceof Number max) { hibernateQuery.maxResults(max.intValue()); } @@ -104,7 +104,7 @@ public class CriteriaMethodInvoker { hibernateQuery.projections().count(); result = hibernateQuery.singleResult(); } else if (builder.isPaginationEnabledList()) { - Map argMap = (Map) args[0]; + Map<?, ?> argMap = (Map<?, ?>) args[0]; final String sortField = (String) argMap.get(HibernateQueryArgument.SORT.value()); if (sortField != null) { final boolean ignoreCase = @@ -115,13 +115,11 @@ public class CriteriaMethodInvoker { ? Query.Order.Direction.DESC : Query.Order.Direction.ASC; Query.Order order; - if (ignoreCase) { - order = new Query.Order(sortField, direction); - order.ignoreCase(); - } else { order = new Query.Order(sortField, direction); + if (ignoreCase) { + order.ignoreCase(); } - hibernateQuery.order(order); + hibernateQuery.order(order); } result = new PagedResultList<>(hibernateQuery); } else if (builder.isScroll()) { @@ -153,7 +151,7 @@ public class CriteriaMethodInvoker { } final boolean hasMoreThanOneArg = args.length > 1; - final Closure callable = hasMoreThanOneArg ? (Closure) args[1] : (Closure) args[0]; + final Closure<?> callable = hasMoreThanOneArg ? (Closure<?>) args[1] : (Closure<?>) args[0]; final HibernateQuery hibernateQuery = builder.getHibernateQuery(); if (method != null) { @@ -185,7 +183,7 @@ public class CriteriaMethodInvoker { final Attribute<?, ?> attribute = entityType.getAttribute(name); if (attribute.isAssociation()) { - Class oldTargetClass = builder.getTargetClass(); + Class<?> oldTargetClass = builder.getTargetClass(); builder.setTargetClass(builder.getClassForAssociationType(attribute)); JoinType joinType; if (hasMoreThanOneArg) { @@ -197,7 +195,7 @@ public class CriteriaMethodInvoker { } hibernateQuery.join(name, joinType); - hibernateQuery.in(name, new DetachedCriteria(builder.getTargetClass()).build(callable)); + hibernateQuery.in(name, new DetachedCriteria<>(builder.getTargetClass()).build(callable)); builder.setTargetClass(oldTargetClass); return name; @@ -216,6 +214,16 @@ public class CriteriaMethodInvoker { switch (method) { case ID_EQUALS: return builder.eq("id", args[0]); + case CACHE: + if (args[0] instanceof Boolean b) { + builder.cache(b); + } + return name; + case READ_ONLY: + if (args[0] instanceof Boolean b) { + builder.readOnly(b); + } + return name; case IS_NULL, IS_NOT_NULL, IS_EMPTY, IS_NOT_EMPTY: if (!(args[0] instanceof String)) { builder.throwRuntimeException( @@ -261,8 +269,8 @@ public class CriteriaMethodInvoker { } break; case EQUALS: - if (args.length == 3 && args[2] instanceof Map) { - return builder.eq(propertyName, args[1], (Map) args[2]); + if (args.length == 3 && args[2] instanceof Map<?, ?> map) { + return builder.eq(propertyName, args[1], map); } return builder.eq(propertyName, args[1]); case EQUALS_PROPERTY: @@ -279,7 +287,7 @@ public class CriteriaMethodInvoker { return builder.ilike(propertyName, args[1]); case IN: if (args[1] instanceof Collection) { - return builder.in(propertyName, (Collection) args[1]); + return builder.in(propertyName, (Collection<?>) args[1]); } else if (args[1] instanceof Object[]) { return builder.in(propertyName, (Object[]) args[1]); } @@ -320,7 +328,7 @@ public class CriteriaMethodInvoker { private boolean isCriteriaConstructionMethod(CriteriaMethods method, Object... args) { return (method == CriteriaMethods.LIST_CALL && args.length == 2 - && args[0] instanceof Map + && args[0] instanceof Map<?, ?> && args[1] instanceof Closure) || (method == CriteriaMethods.ROOT_CALL || method == CriteriaMethods.ROOT_DO_CALL diff --git a/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethods.java b/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethods.java index 11e81c6609..46d0f4b35f 100644 --- a/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethods.java +++ b/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethods.java @@ -57,7 +57,9 @@ public enum CriteriaMethods { COUNT_CALL("count"), GET_CALL("get"), SCROLL_CALL("scroll"), - PROJECTIONS("projections"); + PROJECTIONS("projections"), + CACHE("cache"), + READ_ONLY("readOnly"); private final String name; diff --git a/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java b/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java index 99eba287aa..62221c4df1 100644 --- a/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java +++ b/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java @@ -870,8 +870,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * supported. * @return A Criterion instance */ - @SuppressWarnings("rawtypes") - public Criteria eq(String propertyName, Object propertyValue, Map params) { + public Criteria eq(String propertyName, Object propertyValue, Map<?,?> params) { if (Boolean.TRUE.equals(params.get("ignoreCase"))) { hibernateQuery.like(propertyName, "%" + propertyValue.toString() + "%"); } else { diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java index 47210825f2..8e584fb168 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java @@ -102,7 +102,6 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab } } - @Override public int[] getSqlTypes() { return sqlTypes != null ? sqlTypes.clone() : null; } diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/orm/CriteriaMethodInvokerSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/orm/CriteriaMethodInvokerSpec.groovy index a0f8e7e560..978c64532c 100644 --- a/grails-data-hibernate7/core/src/test/groovy/grails/orm/CriteriaMethodInvokerSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/grails/orm/CriteriaMethodInvokerSpec.groovy @@ -179,6 +179,22 @@ class CriteriaMethodInvokerSpec extends Specification { 1 * builder.eq('id', 42L) } + void "trySimpleCriteria: cache delegates to builder.cache"() { + when: + invoker.trySimpleCriteria('cache', CriteriaMethods.CACHE, [true] as Object[]) + + then: + 1 * builder.cache(true) + } + + void "trySimpleCriteria: readOnly delegates to builder.readOnly"() { + when: + invoker.trySimpleCriteria('readOnly', CriteriaMethods.READ_ONLY, [true] as Object[]) + + then: + 1 * builder.readOnly(true) + } + void "trySimpleCriteria: isNull with String delegates to hibernateQuery.isNull"() { when: invoker.trySimpleCriteria('isNull', CriteriaMethods.IS_NULL, ['branch'] as Object[])
