This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch feat/criteria-handler-context-8.1.x in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 83b6c5d9218fe191dae4afeff83970737171689a Author: Walter Duque de Estrada <[email protected]> AuthorDate: Thu Oct 22 10:53:38 2026 -0500 fix(hibernate7): leave the criteria's orders out of a count query A count has one row whatever the order, and an ORDER BY on a column that is not aggregated fails on H2 and most other databases, so `count()` on a DetachedCriteria or a criteria builder with an `order` threw a SQLGrammarException ("Column ... must be in the GROUP BY list"). Only the paged-list total escaped it, because it clears the orders first. JpaCriteriaQueryCreator now skips the orders when every projection of the query is a count, as `count()` and `countDistinct()` set it up. A query that groups and counts keeps its orders. (cherry picked from commit ebc4b2a4e3bfbb4147513a627575fad7a1639a41) --- .../hibernate/query/JpaCriteriaQueryCreator.java | 16 ++++++++++++++++ .../orm/HibernateCriteriaBuilderDirectSpec.groovy | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/JpaCriteriaQueryCreator.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/JpaCriteriaQueryCreator.java index 6f63253379..879c0016b3 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/JpaCriteriaQueryCreator.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/JpaCriteriaQueryCreator.java @@ -272,6 +272,11 @@ public class JpaCriteriaQueryCreator<T> { @SuppressWarnings("unchecked") private void assignOrderBy(CriteriaQuery<?> cq, JpaQueryContext context) { + if (isCountOnly()) { + // a count returns one row whatever the order, and an ORDER BY on a column that is not aggregated fails + // on H2 and most other databases, so a count query leaves the criteria's orders out + return; + } List<Query.Order> orders = detachedCriteria.getOrders(); if (!orders.isEmpty()) { var jpaOrders = orders.stream() @@ -296,6 +301,17 @@ public class JpaCriteriaQueryCreator<T> { } } + /** + * Whether the query selects nothing but counts, as {@code count()} and {@code countDistinct()} do: such a query + * has one row, so the criteria's orders do not apply to it. A query that groups and counts keeps its orders. + */ + private boolean isCountOnly() { + List<Query.Projection> projectionList = projections.getProjectionList(); + return !projectionList.isEmpty() && projectionList.stream() + .allMatch(projection -> projection instanceof Query.CountProjection || + projection instanceof Query.CountDistinctProjection); + } + private void discoverAliases(List<Query.Criterion> criteria, JpaQueryContext context) { if (criteria == null) return; for (Query.Criterion criterion : criteria) { diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderDirectSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderDirectSpec.groovy index 4943cd042c..01b510191c 100644 --- a/grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderDirectSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderDirectSpec.groovy @@ -389,6 +389,28 @@ class HibernateCriteriaBuilderDirectSpec extends HibernateGormDatastoreSpec { cleanup: PredicateGenerator.clearCustomCriterionHandlers() } + + void "a count query leaves the criteria's orders out"() { + expect: "a count with an order counts every row instead of failing on the order by" + new DetachedCriteria(CriteriaTestEntity).build { order('amount', 'desc') }.count() == 4L + new DetachedCriteria(CriteriaTestEntity).build { + gt('amount', 15) + order('name') + }.count() == 3L + + and: "a row count projection with an order returns the count" + c.list { + projections { rowCount() } + order('amount') + } == [4L] + + and: "the builder's count with an order counts every row" + new HibernateCriteriaBuilder(CriteriaTestEntity, manager.hibernateDatastore.sessionFactory, manager.hibernateDatastore) + .count { order('amount', 'desc') } == 4L + + and: "the order still applies to the list" + new DetachedCriteria(CriteriaTestEntity).build { order('amount', 'desc') }.list()*.name == ['D', 'C', 'B', 'A'] + } } @Entity
