title: Grails GORM - How to get a PagedResultList when using aggregation functions
body: <p>I'm using GORM from grails 1.2.1. No chance of upgrading at this point. This is not a grails app per se; rather it is a webapp that uses groovy and leverages GORM for easy domain model persistence.</p>
  
  <p>I have a query like this:</p>
  
  <pre><code>actionsByUser = UserAction.createCriteria().list() {
                      projections {
                          countDistinct('id', 'userCount')
                          groupProperty('user')
                      }
                      firstResult(offset)
                      maxResults(max)
                      order('userCount', 'desc')
                  }
  </code></pre>
  
  <p>That doesn't return me a PagedResultList with a getTotalCount method which would indicate how many results there are and let me know when to display a next link to show the next page of results.</p>
  
  <p>I tried something like this instead:</p>
  
  <pre><code>actionsByUser = UserAction.createCriteria().list(max:max, offset:offset) {
                      projections {
                          countDistinct('id', 'userCount')
                          groupProperty('user')
                      }
                      order('userCount', 'desc')
                  }
  </code></pre>
  
  <p>i.e. change to pass the max and offset parameters in via an initial Map parameter to <code>list</code>, rather than using the DSL within the Closure.</p>
  
  <p>That fails with:</p>
  
  <pre><code>org.hibernate.QueryException: could not resolve property: userCount of: example.domain.UserAction
  at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67)
  at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:61)
  at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1392)
  at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:54)
  at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1367)
  at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:457)
  at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:417)
  at org.hibernate.criterion.Order.toSqlString(Order.java:68)
  at org.hibernate.loader.criteria.CriteriaQueryTranslator.getOrderBy(CriteriaQueryTranslator.java:371)
  at org.hibernate.loader.criteria.CriteriaJoinWalker.&lt;init&gt;(CriteriaJoinWalker.java:113)
  at org.hibernate.loader.criteria.CriteriaJoinWalker.&lt;init&gt;(CriteriaJoinWalker.java:82)
  at org.hibernate.loader.criteria.CriteriaLoader.&lt;init&gt;(CriteriaLoader.java:91)
  at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1578)
  at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
  at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1025)
  </code></pre>
  
  <p>How can I get a result back from grails that is suitable for doing "page 1 of n" functionality?</p>
  
  <p><strong>UPDATE:</strong> <a href="http://jira.grails.org/browse/GRAILS-3045" rel="nofollow">Raised on Grails JIRA</a> as suggested - I didn't get a response on #grails on freenode.</p>