There is a small difference, and I got caught once by it.
As findAll allows you to write the query anyway you want, which may include the "order by ... desc" fragment. Therefore, findAll() ignores the "sort" and "order" entry in the pagination map. This won't return a list of countries in descending order:
def list = Country.findAll("from Country c where c.continent = ?",
[ continent ],
[sort: 'name', order: 'desc']);
To make it work, I did the following:
def sort = params.sort ?: 'name'
def order = params.order ?: 'asc'
def q = "from Country c where c.continent = ? order by ${sort} ${order}"
def list = Country.findAll(q, [ continent ], params)
The params is still passed in to the function because the max and offset parameters are used.
If the query is simple enough like the query above, it is better to write:
def list = Country.findAllByContinent(continent, params)
No comments:
Post a Comment