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();