开发者

Grails GORM - How to get a PagedResultList when using aggregation functions

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.

I have a query like this:

actionsByUser = UserAction.createCriteria().list() {
                    projections {
                        countDistinct('id', 'userCount')
                        groupProperty('user')
                    }
    开发者_StackOverflow                firstResult(offset)
                    maxResults(max)
                    order('userCount', 'desc')
                }

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.

I tried something like this instead:

actionsByUser = UserAction.createCriteria().list(max:max, offset:offset) {
                    projections {
                        countDistinct('id', 'userCount')
                        groupProperty('user')
                    }
                    order('userCount', 'desc')
                }

i.e. change to pass the max and offset parameters in via an initial Map parameter to list, rather than using the DSL within the Closure.

That fails with:

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.<init>(CriteriaJoinWalker.java:113)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(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)

How can I get a result back from grails that is suitable for doing "page 1 of n" functionality?

UPDATE: Raised on Grails JIRA as suggested - I didn't get a response on #grails on freenode.


It's not documented very well but put your pagination params in the createCriteria.list([in here!]) this should return you a PagedResultList

example...

def result=Thing.createCriteria().list(max:params.max, offset:params.offset){
   maxResults(params.max)
   firstResult(params.offset)
} 

[UPDATE]

Your error seems to be with an invalid query could not resolve property: userCount. Take a second look at your projection query. At first glance it does seem okay and there was a jira issue that seems to be resolved in Grails 1.2.

Also, here are some links explaining the poorly documented PagedResultList...

Here and Here


The problem is HibernateCriteria query does not understand alias defined in projections which is the reason for error "unable to resolve property", the issue yet to be fixed

here is the jira ticket http://jira.grails.org/browse/GRAILS-3875

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜