tags:

views:

38

answers:

1

I have a User class and a Item class and a user can have multiple items.

I want to select some users based on other property using finndAllByProperty and adding the pagination parameters (see http://www.grails.org/doc/1.2.2/ref/Domain%20Classes/findAllBy.html .)

The problem is that I want to sort the result based on how many items each user has So I would like to make something like:

myUsers = User.findAllByProperty(propertyInstance,[max:10, offset:offset, sort:'items.size()', order:"desc"])

but of course that "sort:'items.size()' " does not work. Is it any way to do this without adding a itemsNr property in the User domain that will update on each items added/removed ?

+1  A: 

Have you tried sort:'count(items)'? HQL does provide that method for selecting the size of a collection but I'm not sure if it will work via findAllBy*

If that doesn't work you'll need to try it use the CriteriaBuilder to achieve what you want.

cheers

Lee

leebutts
it does not work, I'm afraid.
cripox
I will go with CriteriaBuilder, thanks for the answer anyway.
cripox