Skip to content

Commit

Permalink
Merged fix for GRAILS-8365
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Nov 30, 2011
1 parent 3fd89b7 commit e11436e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
22 changes: 14 additions & 8 deletions grails-hibernate/src/main/groovy/grails/orm/PagedResultList.java
Expand Up @@ -27,6 +27,7 @@
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.impl.CriteriaImpl;
import org.springframework.orm.hibernate3.HibernateCallback;

/**
Expand Down Expand Up @@ -159,14 +160,19 @@ public List subList(int i, int i1) {
public int getTotalCount() {
if(totalCount == Integer.MIN_VALUE) {
totalCount = (Integer)hibernateTemplate.execute(new HibernateCallback<Object>() {
public Object doInHibernate(Session session)
throws HibernateException,
SQLException {
hibernateTemplate.applySettings(criteria);
criteria.setFirstResult(0);
criteria.setProjection(Projections.rowCount());
int totalCount = ((Number)criteria.uniqueResult()).intValue();
return totalCount;
public Object doInHibernate(Session session) throws HibernateException, SQLException {
CriteriaImpl impl = (CriteriaImpl) criteria;
Criteria totalCriteria = session.createCriteria(impl.getEntityOrClassName());
hibernateTemplate.applySettings(totalCriteria);

Iterator iterator = impl.iterateExpressionEntries();
while (iterator.hasNext()) {
CriteriaImpl.CriterionEntry entry = (CriteriaImpl.CriterionEntry) iterator.next();
totalCriteria.add(entry.getCriterion());
}
totalCriteria.setProjection(impl.getProjection());
totalCriteria.setProjection(Projections.rowCount());
return ((Number)totalCriteria.uniqueResult()).intValue();
}
});
}
Expand Down
@@ -0,0 +1,36 @@
package org.codehaus.groovy.grails.orm.hibernate

import grails.persistence.Entity

/**
* Tests usage of total count when an order by is used
*/
class TotalCountWithOrderSpec extends GormSpec{

void "Test total with with order by"() {
when:"A pagination query is used with order by"
def criteria = Post.createCriteria()
def results = criteria.list (max: 10, offset: 5) {
order("dateCreated", "desc")
}

then:"Both total count and results work"
results.size() == 0
results.totalCount == 0
}

@Override
List getDomainClasses() {
[Post]
}
}
@Entity
class Post {

String title
Date dateCreated

static constraints = {
}
}

0 comments on commit e11436e

Please sign in to comment.