Question

My grails app has the following classes

class Person {
    Address address
    // other attributes
}

class Address {
    String street
    City city
    // more attributes
}

I would like to query the first 5 people alphabetically by street name. Currently I do something like

def criteria = Person.createCriteria();
def people = criteria.list(max:5) {
    address {
       order("street","asc")
    }
}

This works. I'm just wondering if there's a shorter way to do this (possibly without the criteria builder).

Était-ce utile?

La solution

Actually I think this is the clearest and most efficient way of doing it. You could try things like executeQuery, but to be honest, I'm not sure if that's any less verbose. If you're just trying to shorten the code, you could simplify the first two lines:

def people = Person.createCriteria().list(max:5) {
...
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top