Jeff Storey
Jeff Storey

Reputation: 57202

grails query on nested attributes without criteria builder

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).

Upvotes: 2

Views: 611

Answers (1)

Igor
Igor

Reputation: 34011

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) {
...

Upvotes: 2

Related Questions