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 a33dcf010d19f0fa44ecf1bd1254b1b39c746954 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Sat Mar 7 22:44:23 2026 -0600 hibernate7: - Fix alias resolution architecture: - Fix scroll(): - Fix countDistinct/groupProperty - Add eq(Map, String, Object) overload for Groovy named-params form, --- .../groovy/grails/orm/CriteriaMethodInvoker.java | 51 ++++---- .../grails/orm/HibernateCriteriaBuilder.java | 142 ++++++++------------- .../orm/hibernate/HibernateGormStaticApi.groovy | 4 +- .../grails/orm/hibernate/query/HibernateQuery.java | 82 ++++++------ .../grails/orm/CriteriaMethodInvokerSpec.groovy | 26 +--- .../grails/orm/HibernateCriteriaBuilderSpec.groovy | 21 +++ 6 files changed, 144 insertions(+), 182 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 9dae907053..dcfd3c8d3f 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 @@ -123,6 +123,8 @@ public class CriteriaMethodInvoker { hibernateQuery.order(order); } result = new PagedResultList<>(hibernateQuery); + } else if (builder.isScroll()) { + result = hibernateQuery.scroll(); } else { result = hibernateQuery.list(); } @@ -224,13 +226,13 @@ public class CriteriaMethodInvoker { final String value = (String) args[0]; switch (method) { case IS_NULL -> - builder.getHibernateQuery().isNull(builder.calculatePropertyName(value)); + builder.getHibernateQuery().isNull(value); case IS_NOT_NULL -> - builder.getHibernateQuery().isNotNull(builder.calculatePropertyName(value)); + builder.getHibernateQuery().isNotNull(value); case IS_EMPTY -> - builder.getHibernateQuery().isEmpty(builder.calculatePropertyName(value)); + builder.getHibernateQuery().isEmpty(value); case IS_NOT_EMPTY -> - builder.getHibernateQuery().isNotEmpty(builder.calculatePropertyName(value)); + builder.getHibernateQuery().isNotEmpty(value); } return name; } @@ -246,54 +248,53 @@ public class CriteriaMethodInvoker { switch (method) { case RLIKE: - return builder.rlike(builder.calculatePropertyName(propertyName), args[1]); + return builder.rlike(propertyName, args[1]); case BETWEEN: if (args.length >= 3) { - return builder.between(builder.calculatePropertyName(propertyName), args[1], args[2]); + return builder.between(propertyName, args[1], args[2]); } break; case EQUALS: if (args.length == 3 && args[2] instanceof Map) { - return builder.eq(builder.calculatePropertyName(propertyName), args[1], (Map) args[2]); + return builder.eq(propertyName, args[1], (Map) args[2]); } - return builder.eq(builder.calculatePropertyName(propertyName), args[1]); + return builder.eq(propertyName, args[1]); case EQUALS_PROPERTY: - return builder.eqProperty(builder.calculatePropertyName(propertyName), args[1].toString()); + return builder.eqProperty(propertyName, args[1].toString()); case GREATER_THAN: - return builder.gt(builder.calculatePropertyName(propertyName), args[1]); + return builder.gt(propertyName, args[1]); case GREATER_THAN_PROPERTY: - return builder.gtProperty(builder.calculatePropertyName(propertyName), args[1].toString()); + return builder.gtProperty(propertyName, args[1].toString()); case GREATER_THAN_OR_EQUAL: - return builder.ge(builder.calculatePropertyName(propertyName), args[1]); + return builder.ge(propertyName, args[1]); case GREATER_THAN_OR_EQUAL_PROPERTY: - return builder.geProperty(builder.calculatePropertyName(propertyName), args[1].toString()); + return builder.geProperty(propertyName, args[1].toString()); case ILIKE: - return builder.ilike(builder.calculatePropertyName(propertyName), args[1]); + return builder.ilike(propertyName, args[1]); case IN: if (args[1] instanceof Collection) { - return builder.in(builder.calculatePropertyName(propertyName), (Collection) args[1]); + return builder.in(propertyName, (Collection) args[1]); } else if (args[1] instanceof Object[]) { - return builder.in(builder.calculatePropertyName(propertyName), (Object[]) args[1]); + return builder.in(propertyName, (Object[]) args[1]); } break; case LESS_THAN: - return builder.lt(builder.calculatePropertyName(propertyName), args[1]); + return builder.lt(propertyName, args[1]); case LESS_THAN_PROPERTY: - return builder.ltProperty(builder.calculatePropertyName(propertyName), args[1].toString()); + return builder.ltProperty(propertyName, args[1].toString()); case LESS_THAN_OR_EQUAL: - return builder.le(builder.calculatePropertyName(propertyName), args[1]); + return builder.le(propertyName, args[1]); case LESS_THAN_OR_EQUAL_PROPERTY: - return builder.leProperty(builder.calculatePropertyName(propertyName), args[1].toString()); + return builder.leProperty(propertyName, args[1].toString()); case LIKE: - return builder.like(builder.calculatePropertyName(propertyName), args[1]); + return builder.like(propertyName, args[1]); case NOT_EQUAL: - return builder.ne(builder.calculatePropertyName(propertyName), args[1]); + return builder.ne(propertyName, args[1]); case NOT_EQUAL_PROPERTY: - return builder.neProperty(builder.calculatePropertyName(propertyName), args[1].toString()); + return builder.neProperty(propertyName, args[1].toString()); case SIZE_EQUALS: if (args[1] instanceof Number) { - return builder.sizeEq( - builder.calculatePropertyName(propertyName), ((Number) args[1]).intValue()); + return builder.sizeEq(propertyName, ((Number) args[1]).intValue()); } break; } 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 f157479d7c..f381729239 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 @@ -9,12 +9,11 @@ * * https://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package grails.orm; @@ -26,10 +25,8 @@ import groovy.lang.GroovyObjectSupport; import groovy.util.logging.Slf4j; import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.JoinType; -import jakarta.persistence.criteria.Root; import jakarta.persistence.metamodel.Attribute; import jakarta.persistence.metamodel.PluralAttribute; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -46,11 +43,7 @@ import org.grails.orm.hibernate.HibernateSession; import org.grails.orm.hibernate.GrailsHibernateTemplate; import org.grails.orm.hibernate.query.HibernateQuery; import org.hibernate.FetchMode; -import org.hibernate.Session; import org.hibernate.SessionFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.core.convert.ConversionService; /** * Implements the GORM criteria DSL for Hibernate 7+. The builder exposes a Groovy-closure DSL that @@ -101,36 +94,28 @@ import org.springframework.core.convert.ConversionService; */ @Slf4j @SuppressWarnings("PMD.AvoidDuplicateLiterals") -public class HibernateCriteriaBuilder extends GroovyObjectSupport - implements BuildableCriteria, ProjectionList { +public class HibernateCriteriaBuilder extends GroovyObjectSupport implements BuildableCriteria, ProjectionList { /* * Define constants which may be used inside of criteria queries * to refer to standard Hibernate Type instances. */ - private static final Logger log = LoggerFactory.getLogger(HibernateCriteriaBuilder.class); - protected SessionFactory sessionFactory; - protected Session hibernateSession; - protected Class<?> targetClass; - protected CriteriaQuery criteriaQuery; - protected boolean uniqueResult = false; - protected boolean participate; + private final SessionFactory sessionFactory; + private Class<?> targetClass; + private CriteriaQuery<?> criteriaQuery; + private boolean uniqueResult = false; + private boolean participate; @SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName") - protected boolean scroll; + private boolean scroll; @SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName") - protected boolean count; - - protected List<String> aliasStack = new ArrayList<String>(); - protected static final String ALIAS = "_alias"; - protected boolean paginationEnabledList = false; - protected ConversionService conversionService; - protected int defaultFlushMode; - protected HibernateDatastore datastore; - protected org.hibernate.query.criteria.HibernateCriteriaBuilder cb; - protected Root root; - protected HibernateQuery hibernateQuery; + private boolean count; + + private boolean paginationEnabledList = false; + private int defaultFlushMode; + private final org.hibernate.query.criteria.HibernateCriteriaBuilder cb; + private final HibernateQuery hibernateQuery; private boolean shouldLock; private boolean shouldCache; @@ -138,7 +123,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport private boolean readOnly; @SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName") - protected boolean distinct = false; + private boolean distinct = false; @SuppressWarnings("rawtypes") public HibernateCriteriaBuilder( @@ -155,16 +140,12 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport } public final void setDatastore(HibernateDatastore datastore) { - this.datastore = datastore; if (MultiTenant.class.isAssignableFrom(targetClass) && datastore.getMultiTenancyMode() == MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR) { datastore.enableMultiTenancyFilter(); } } - public void setConversionService(ConversionService conversionService) { - this.conversionService = conversionService; - } /** * A projection that selects a property name @@ -259,30 +240,11 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport return this; } - /** - * Calculates the property name including any alias paths - * - * @param propertyName The property name - * @return The calculated property name - */ - protected String calculatePropertyName(String propertyName) { - return propertyName; - } - - private String getLastAlias() { - if (aliasStack.size() > 0) { - return aliasStack.get(aliasStack.size() - 1).toString(); - } - return null; - } public Class<?> getTargetClass() { return targetClass; } - protected DetachedCriteria convertToHibernateCriteria(QueryableCriteria<?> queryableCriteria) { - return null; - } /** * Adds a projection that allows the criteria to return the property count @@ -619,7 +581,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport @Override public Criteria gtSome(String propertyName, Closure<?> propertyValue) { - return gtSome(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); + return gtSome(propertyName, new DetachedCriteria<>(targetClass).build(propertyValue)); } @Override @@ -630,7 +592,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport @Override public Criteria geSome(String propertyName, Closure<?> propertyValue) { - return geSome(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); + return geSome(propertyName, new DetachedCriteria<>(targetClass).build(propertyValue)); } @Override @@ -641,7 +603,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport @Override public Criteria ltSome(String propertyName, Closure<?> propertyValue) { - return ltSome(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); + return ltSome(propertyName, new DetachedCriteria<>(targetClass).build(propertyValue)); } @Override @@ -652,7 +614,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport @Override public Criteria leSome(String propertyName, Closure<?> propertyValue) { - return leSome(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); + return leSome(propertyName, new DetachedCriteria<>(targetClass).build(propertyValue)); } @Override @@ -668,12 +630,12 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport @Override public Criteria in(String propertyName, Closure<?> subquery) { - return inList(propertyName, new DetachedCriteria(targetClass).build(subquery)); + return inList(propertyName, new DetachedCriteria<>(targetClass).build(subquery)); } @Override public Criteria inList(String propertyName, Closure<?> subquery) { - return inList(propertyName, new DetachedCriteria(targetClass).build(subquery)); + return inList(propertyName, new DetachedCriteria<>(targetClass).build(subquery)); } @Override @@ -684,7 +646,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport @Override public Criteria notIn(String propertyName, Closure<?> subquery) { - return notIn(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(subquery)); + return notIn(propertyName, new DetachedCriteria<>(targetClass).build(subquery)); } /** @@ -850,20 +812,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport return eq("id", o); } - /** - * Groovy moves the map to the first parameter if using the idiomatic form, e.g. <code> - * eq 'firstName', 'Fred', ignoreCase: true</code>. - * - * @param params optional map with customization parameters; currently only 'ignoreCase' is - * supported. - * @param propertyName - * @param propertyValue - * @return A Criterion instance - */ - @SuppressWarnings("rawtypes") - public Criteria eq(Map params, String propertyName, Object propertyValue) { - return eq(propertyName, propertyValue, params); - } + /** * Creates an "equals" Criterion based on the specified property name and value. Supports @@ -886,6 +835,11 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport return this; } + @SuppressWarnings("rawtypes") + public Criteria eq(Map params, String propertyName, Object propertyValue) { + return eq(propertyName, propertyValue, params); + } + /** * Creates a Criterion with from the specified property name and "like" expression * @@ -1104,7 +1058,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport @Override public Object list(@DelegatesTo(Criteria.class) Closure c) { - hibernateQuery.setDetachedCriteria(new DetachedCriteria(targetClass)); + hibernateQuery.setDetachedCriteria(new DetachedCriteria<>(targetClass)); return invokeMethod(CriteriaMethods.LIST_CALL.getName(), new Object[] {c}); } @@ -1118,7 +1072,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport @Override public Object list(Map params, @DelegatesTo(Criteria.class) Closure c) { - hibernateQuery.setDetachedCriteria(new DetachedCriteria(targetClass)); + hibernateQuery.setDetachedCriteria(new DetachedCriteria<>(targetClass)); return invokeMethod(CriteriaMethods.LIST_CALL.getName(), new Object[] {params, c}); } @@ -1155,12 +1109,22 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport return new CriteriaMethodInvoker(this).invokeMethod(name, args); } + @Override + public Object getProperty(String propertyName) { + return super.getProperty(propertyName); + } + + @Override + public void setProperty(String propertyName, Object newValue) { + super.setProperty(propertyName, newValue); + } + /** * Returns the criteria instance * * @return The criteria instance */ - public CriteriaQuery getInstance() { + public CriteriaQuery<?> getInstance() { return criteriaQuery; } @@ -1193,6 +1157,10 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport this.scroll = scroll; } + public boolean isScroll() { + return scroll; + } + public void setCount(boolean count) { this.count = count; } @@ -1221,15 +1189,15 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport return cb; } - protected Class getClassForAssociationType(Attribute<?, ?> type) { + public Class<?> getClassForAssociationType(Attribute<?, ?> type) { if (type instanceof PluralAttribute) { - return ((PluralAttribute) type).getElementType().getJavaType(); + return ((PluralAttribute<?,?,?>) type).getElementType().getJavaType(); } return type.getJavaType(); } /** Throws a runtime exception where necessary to ensure the session gets closed */ - protected void throwRuntimeException(RuntimeException t) { + public void throwRuntimeException(RuntimeException t) { closeSessionFollowingException(); throw t; } @@ -1241,10 +1209,8 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport } /** Closes the session if it is copen */ - protected void closeSession() { - if (hibernateSession != null && hibernateSession.isOpen() && !participate) { - hibernateSession.close(); - } + public void closeSession() { + hibernateQuery.getSession().disconnect(); } public int getDefaultFlushMode() { diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy index c56a484d2f..b0ad1678cb 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy @@ -459,9 +459,7 @@ class HibernateGormStaticApi<D> extends GormStaticApi<D> { @Override GrailsCriteria createCriteria() { - def builder = new HibernateCriteriaBuilder(persistentClass, sessionFactory, (HibernateDatastore) datastore) - builder.conversionService = conversionService - return builder + return new HibernateCriteriaBuilder(persistentClass, sessionFactory, (HibernateDatastore) datastore) } protected void firePostQueryEvent(Object result) { diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java index 985dc3d2ae..7600503b27 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java @@ -92,13 +92,13 @@ public class HibernateQuery extends Query { @Override public Query isEmpty(String property) { - detachedCriteria.isEmpty(property); + detachedCriteria.isEmpty(calculatePropertyName(property)); return this; } @Override public Query isNotEmpty(String property) { - detachedCriteria.isNotEmpty(property); + detachedCriteria.isNotEmpty(calculatePropertyName(property)); return this; } @@ -109,13 +109,13 @@ public class HibernateQuery extends Query { @Override public Query isNull(String property) { - detachedCriteria.isNull(property); + detachedCriteria.isNull(calculatePropertyName(property)); return this; } @Override public Query isNotNull(String property) { - detachedCriteria.isNotNull(property); + detachedCriteria.isNotNull(calculatePropertyName(property)); return this; } @@ -166,7 +166,7 @@ public class HibernateQuery extends Query { @Override public Query eq(String property, Object value) { - detachedCriteria.eq(property, value); + detachedCriteria.eq(calculatePropertyName(property), value); return this; } @@ -178,7 +178,7 @@ public class HibernateQuery extends Query { @Override public Query gt(String property, Object value) { - detachedCriteria.gt(property, value); + detachedCriteria.gt(calculatePropertyName(property), value); return this; } @@ -246,67 +246,67 @@ public class HibernateQuery extends Query { @Override public Query allEq(Map<String, Object> values) { values.forEach( - (key, value) -> detachedCriteria.eq(key, value)); + (key, value) -> detachedCriteria.eq(calculatePropertyName(key), value)); return this; } @Override public Query ge(String property, Object value) { - detachedCriteria.ge(property, value); + detachedCriteria.ge(calculatePropertyName(property), value); return this; } @Override public Query le(String property, Object value) { - detachedCriteria.le(property, value); + detachedCriteria.le(calculatePropertyName(property), value); return this; } @Override public Query gte(String property, Object value) { - detachedCriteria.gte(property, value); + detachedCriteria.gte(calculatePropertyName(property), value); return this; } @Override public Query lte(String property, Object value) { - detachedCriteria.lte(property, value); + detachedCriteria.lte(calculatePropertyName(property), value); return this; } @Override public Query lt(String property, Object value) { - detachedCriteria.lt(property, value); + detachedCriteria.lt(calculatePropertyName(property), value); return this; } @Override public Query in(String property, List values) { - detachedCriteria.in(property, values); + detachedCriteria.in(calculatePropertyName(property), values); return this; } @Override public Query between(String property, Object start, Object end) { - detachedCriteria.between(property, start, end); + detachedCriteria.between(calculatePropertyName(property), start, end); return this; } @Override public Query like(String property, String expr) { - detachedCriteria.like(property, expr); + detachedCriteria.like(calculatePropertyName(property), expr); return this; } @Override public Query ilike(String property, String expr) { - detachedCriteria.ilike(property, expr); + detachedCriteria.ilike(calculatePropertyName(property), expr); return this; } @Override public Query rlike(String property, String expr) { - detachedCriteria.rlike(property, expr); + detachedCriteria.rlike(calculatePropertyName(property), expr); return this; } @@ -472,7 +472,7 @@ public class HibernateQuery extends Query { } public Query in(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.inList(propertyName, subquery); + detachedCriteria.inList(calculatePropertyName(propertyName), subquery); return this; } @@ -493,7 +493,7 @@ public class HibernateQuery extends Query { } public Query notIn(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.notIn(propertyName, subquery); + detachedCriteria.notIn(calculatePropertyName(propertyName), subquery); return this; } @@ -508,112 +508,112 @@ public class HibernateQuery extends Query { } public Query gtAll(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.gtAll(propertyName, subquery); + detachedCriteria.gtAll(calculatePropertyName(propertyName), subquery); return this; } public Query geAll(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.geAll(propertyName, subquery); + detachedCriteria.geAll(calculatePropertyName(propertyName), subquery); return this; } public Query ltAll(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.ltAll(propertyName, subquery); + detachedCriteria.ltAll(calculatePropertyName(propertyName), subquery); return this; } public Query leAll(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.leAll(propertyName, subquery); + detachedCriteria.leAll(calculatePropertyName(propertyName), subquery); return this; } public Query gtSome(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.gtSome(propertyName, subquery); + detachedCriteria.gtSome(calculatePropertyName(propertyName), subquery); return this; } public Query geSome(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.geSome(propertyName, subquery); + detachedCriteria.geSome(calculatePropertyName(propertyName), subquery); return this; } public Query ltSome(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.ltSome(propertyName, subquery); + detachedCriteria.ltSome(calculatePropertyName(propertyName), subquery); return this; } public Query leSome(String propertyName, QueryableCriteria<?> subquery) { - detachedCriteria.leSome(propertyName, subquery); + detachedCriteria.leSome(calculatePropertyName(propertyName), subquery); return this; } public Query eqAll(String propertyName, QueryableCriteria propertyValue) { - detachedCriteria.eqAll(propertyName, propertyValue); + detachedCriteria.eqAll(calculatePropertyName(propertyName), propertyValue); return this; } public Query ne(String propertyName, Object propertyValue) { - detachedCriteria.ne(propertyName, propertyValue); + detachedCriteria.ne(calculatePropertyName(propertyName), propertyValue); return this; } public Query eqProperty(String propertyName, String otherPropertyName) { - detachedCriteria.eqProperty(propertyName, otherPropertyName); + detachedCriteria.eqProperty(calculatePropertyName(propertyName), otherPropertyName); return this; } public Query neProperty(String propertyName, String otherPropertyName) { - detachedCriteria.neProperty(propertyName, otherPropertyName); + detachedCriteria.neProperty(calculatePropertyName(propertyName), otherPropertyName); return this; } public Query gtProperty(String propertyName, String otherPropertyName) { - detachedCriteria.gtProperty(propertyName, otherPropertyName); + detachedCriteria.gtProperty(calculatePropertyName(propertyName), otherPropertyName); return this; } public Query geProperty(String propertyName, String otherPropertyName) { - detachedCriteria.geProperty(propertyName, otherPropertyName); + detachedCriteria.geProperty(calculatePropertyName(propertyName), otherPropertyName); return this; } public Query ltProperty(String propertyName, String otherPropertyName) { - detachedCriteria.ltProperty(propertyName, otherPropertyName); + detachedCriteria.ltProperty(calculatePropertyName(propertyName), otherPropertyName); return this; } public Query leProperty(String propertyName, String otherPropertyName) { - detachedCriteria.leProperty(propertyName, otherPropertyName); + detachedCriteria.leProperty(calculatePropertyName(propertyName), otherPropertyName); return this; } public Query sizeEq(String propertyName, int size) { - detachedCriteria.sizeEq(propertyName, size); + detachedCriteria.sizeEq(calculatePropertyName(propertyName), size); return this; } public Query sizeGt(String propertyName, int size) { - detachedCriteria.sizeGt(propertyName, size); + detachedCriteria.sizeGt(calculatePropertyName(propertyName), size); return this; } public Query sizeGe(String propertyName, int size) { - detachedCriteria.sizeGe(propertyName, size); + detachedCriteria.sizeGe(calculatePropertyName(propertyName), size); return this; } public Query sizeLe(String propertyName, int size) { - detachedCriteria.sizeLe(propertyName, size); + detachedCriteria.sizeLe(calculatePropertyName(propertyName), size); return this; } public Query sizeLt(String propertyName, int size) { - detachedCriteria.sizeLt(propertyName, size); + detachedCriteria.sizeLt(calculatePropertyName(propertyName), size); return this; } public Query sizeNe(String propertyName, int size) { - detachedCriteria.sizeNe(propertyName, size); + detachedCriteria.sizeNe(calculatePropertyName(propertyName), size); return this; } 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 3f9c34f5da..a0f8e7e560 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 @@ -184,7 +184,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.trySimpleCriteria('isNull', CriteriaMethods.IS_NULL, ['branch'] as Object[]) then: - 1 * builder.calculatePropertyName('branch') >> 'branch' 1 * query.isNull('branch') } @@ -193,7 +192,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.trySimpleCriteria('isNotNull', CriteriaMethods.IS_NOT_NULL, ['branch'] as Object[]) then: - 1 * builder.calculatePropertyName('branch') >> 'branch' 1 * query.isNotNull('branch') } @@ -202,7 +200,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.trySimpleCriteria('isEmpty', CriteriaMethods.IS_EMPTY, ['transactions'] as Object[]) then: - 1 * builder.calculatePropertyName('transactions') >> 'transactions' 1 * query.isEmpty('transactions') } @@ -211,7 +208,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.trySimpleCriteria('isNotEmpty', CriteriaMethods.IS_NOT_EMPTY, ['transactions'] as Object[]) then: - 1 * builder.calculatePropertyName('transactions') >> 'transactions' 1 * query.isNotEmpty('transactions') } @@ -232,7 +228,7 @@ class CriteriaMethodInvokerSpec extends Specification { then: result != null // UNHANDLED sentinel object - 0 * builder.calculatePropertyName(_) + 0 * query.isNull(_) } void "trySimpleCriteria: null method returns UNHANDLED"() { @@ -251,7 +247,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.RLIKE, ['firstName', '^F.*'] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.rlike('firstName', '^F.*') } @@ -260,7 +255,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.BETWEEN, ['balance', 10, 100] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.between('balance', 10, 100) } @@ -269,7 +263,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.EQUALS, ['firstName', 'Fred'] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.eq('firstName', 'Fred') } @@ -281,7 +274,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.EQUALS, ['firstName', 'Fred', params] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.eq('firstName', 'Fred', params) } @@ -290,7 +282,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.EQUALS_PROPERTY, ['firstName', 'lastName'] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.eqProperty('firstName', 'lastName') } @@ -299,7 +290,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.GREATER_THAN, ['balance', 100] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.gt('balance', 100) } @@ -308,7 +298,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.GREATER_THAN_PROPERTY, ['balance', 'balance'] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.gtProperty('balance', 'balance') } @@ -317,7 +306,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.GREATER_THAN_OR_EQUAL, ['balance', 100] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.ge('balance', 100) } @@ -326,7 +314,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.GREATER_THAN_OR_EQUAL_PROPERTY, ['balance', 'balance'] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.geProperty('balance', 'balance') } @@ -335,7 +322,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.ILIKE, ['firstName', 'fr%'] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.ilike('firstName', 'fr%') } @@ -347,7 +333,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.IN, ['firstName', names] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.in('firstName', names) } @@ -359,7 +344,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.IN, ['firstName', names] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.in('firstName', names) } @@ -368,7 +352,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.LESS_THAN, ['balance', 500] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.lt('balance', 500) } @@ -377,7 +360,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.LESS_THAN_PROPERTY, ['balance', 'balance'] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.ltProperty('balance', 'balance') } @@ -386,7 +368,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.LESS_THAN_OR_EQUAL, ['balance', 500] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.le('balance', 500) } @@ -395,7 +376,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.LESS_THAN_OR_EQUAL_PROPERTY, ['balance', 'balance'] as Object[]) then: - 1 * builder.calculatePropertyName('balance') >> 'balance' 1 * builder.leProperty('balance', 'balance') } @@ -404,7 +384,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.LIKE, ['firstName', 'Fr%'] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.like('firstName', 'Fr%') } @@ -413,7 +392,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.NOT_EQUAL, ['firstName', 'Fred'] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.ne('firstName', 'Fred') } @@ -422,7 +400,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.NOT_EQUAL_PROPERTY, ['firstName', 'lastName'] as Object[]) then: - 1 * builder.calculatePropertyName('firstName') >> 'firstName' 1 * builder.neProperty('firstName', 'lastName') } @@ -431,7 +408,6 @@ class CriteriaMethodInvokerSpec extends Specification { invoker.tryPropertyCriteria(CriteriaMethods.SIZE_EQUALS, ['transactions', 2] as Object[]) then: - 1 * builder.calculatePropertyName('transactions') >> 'transactions' 1 * builder.sizeEq('transactions', 2) } diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderSpec.groovy index 91dc5f8384..fa297dea84 100644 --- a/grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderSpec.groovy @@ -462,6 +462,27 @@ class HibernateCriteriaBuilderSpec extends HibernateGormDatastoreSpec { results.size() == 1 results[0].firstName == "Fred" } + + void "scroll returns a ScrollableResults cursor over matching rows"() { + when: + def results = c.scroll { + eq("lastName", "Flintstone") + order("firstName", "asc") + } + + then: + results instanceof org.hibernate.ScrollableResults + results.next() + results.get().firstName == "Fred" + results.next() + results.get().firstName == "Pebbles" + results.next() + results.get().firstName == "Wilma" + !results.next() + + cleanup: + results?.close() + } } @Entity
