开发者

Grails: findAllBy dynamic method - advanced sort property

开发者 https://www.devze.com 2023-01-15 18:55 出处:网络
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 paramete

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 ?


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.

0

精彩评论

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