Wednesday, April 01, 2009

Difference between findAll and findAllBy

You may have already known the obvious difference between the domain class findAll() and findAllByXXX() method. findAll() expects a query as the first parameter, followed by the parameter list and the pagination parameters whereas findAllByXXX() expects variable list of objects, ending with an optional map for pagination purpose.

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: