From 6b16e836cdea495bca32ae1f1922cad8faef0589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scheibe?= Date: Sun, 1 Oct 2023 18:53:01 +0200 Subject: [PATCH] Fix getTotalCount() with Hibernate for sorting parameters Don't have sorting in the generated query. Otherwise, depending on the database and its strictness configuration, the query might fail. The `PagedResultSpec` in `grails-datastore-gorm-tck` covers this. --- .../groovy/org/grails/orm/hibernate/query/PagedResultList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/PagedResultList.java b/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/PagedResultList.java index 95b49d0d..0501176c 100644 --- a/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/PagedResultList.java +++ b/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/PagedResultList.java @@ -58,7 +58,7 @@ public int getTotalCount() { if (totalCount == Integer.MIN_VALUE) { totalCount = hibernateTemplate.execute(new GrailsHibernateTemplate.HibernateCallback() { public Integer doInHibernate(Session session) throws HibernateException, SQLException { - final CriteriaQuery finalQuery = criteriaQuery.select(criteriaBuilder.count(queryRoot)).distinct(true); + final CriteriaQuery finalQuery = criteriaQuery.select(criteriaBuilder.count(queryRoot)).distinct(true).orderBy(); final Query query = session.createQuery(finalQuery); hibernateTemplate.applySettings(query); return ((Number)query.uniqueResult()).intValue();