You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/10/05 14:43:08 UTC

(grails-core) 01/01: fix: ignore max and offset when counting results

This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch fix/hibernate7-count-ignores-paging-8.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit fc9ae73ddc96944860798d829e9f7ac4355919af
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Oct 5 08:07:19 2026 -0500

    fix: ignore max and offset when counting results
    
    countResults ran through the paging executor, so a count with an offset
    past the single aggregate row returned null instead of the total. A
    count-specific executor without offset/max now serves both branches,
    restoring the Hibernate 5 contract that max and offset are ignored by
    count(). DetachedCriteriaJoinSpec gains the offset(10) assertions (null
    before the fix, 4 after).
    
    Co-Authored-By: Claude Sonnet 5.5 <no...@anthropic.com>
---
 .../groovy/org/grails/orm/hibernate/query/HibernateQuery.java | 11 +++++++++--
 .../tests/detachedcriteria/DetachedCriteriaJoinSpec.groovy    |  2 ++
 2 files changed, 11 insertions(+), 2 deletions(-)

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 ef33c9a3ba..23f44619a6 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
@@ -488,6 +488,12 @@ public class HibernateQuery extends Query {
                 offset, max, lockResult, queryCache, fetchSize, timeout, flushMode, readOnly, proxyHandler);
     }
 
+    /** An executor that never pages, because max and offset do not apply to a count. */
+    private HibernateQueryExecutor getCountQueryExecutor() {
+        return new HibernateQueryExecutor(
+                null, null, lockResult, queryCache, fetchSize, timeout, flushMode, readOnly, proxyHandler);
+    }
+
     private JpaCriteriaQueryCreator<?> createJpaCriteriaQueryCreator() {
         ConversionService conversionService = getSession().getMappingContext().getConversionService();
         return new JpaCriteriaQueryCreator<>(
@@ -526,7 +532,8 @@ public class HibernateQuery extends Query {
         Number result;
         if (projections.getProjectionList().isEmpty()) {
             projections().count();
-            result = (Number) executeSingleResult();
+            var creator = createJpaCriteriaQueryCreator();
+            result = (Number) getCountQueryExecutor().singleResult(getCurrentSession(), creator.createQuery(), creator.getParameterValues());
         } else {
             HibernateCriteriaBuilder cb = getCriteriaBuilder();
 
@@ -539,7 +546,7 @@ public class HibernateQuery extends Query {
 
             countQuery.from(innerSubquery);
             countQuery.select(cb.count(cb.literal(1)));
-            result = (Number) getHibernateQueryExecutor().singleResult(getCurrentSession(), countQuery, creator.getParameterValues());
+            result = (Number) getCountQueryExecutor().singleResult(getCurrentSession(), countQuery, creator.getParameterValues());
         }
 
         return (Number) firePostQueryEvent(result);
diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/detachedcriteria/DetachedCriteriaJoinSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/detachedcriteria/DetachedCriteriaJoinSpec.groovy
index 89b82c5c9e..1d1b906d02 100644
--- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/detachedcriteria/DetachedCriteriaJoinSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/detachedcriteria/DetachedCriteriaJoinSpec.groovy
@@ -46,6 +46,8 @@ class DetachedCriteriaJoinSpec extends HibernateGormDatastoreSpec {
         new DetachedCriteria<>(Club).max(10).offset(0).count() == 4
         Club.where {}.max(2).offset(0).count() == 4
         new DetachedCriteria<>(Club).max(2).offset(0).count() == 4
+        Club.where {}.max(10).offset(10).count() == 4
+        new DetachedCriteria<>(Club).max(10).offset(10).count() == 4
     }
 
     def 'check if inner join is applied correctly'(){