Grails: Problem with nested associations in criteria builder

Posted by Mr.B on Stack Overflow See other posts from Stack Overflow or by Mr.B
Published on 2010-03-16T09:52:07Z Indexed on 2010/03/17 2:31 UTC
Read the original article Hit count: 456

I have a frustrating problem with the criteria builder. I have an application in which one user has one calendar, and a calendar has many entries. Seems straightforward enough, but when I try to get the calendar entries for a given user, I can't access the user property (MissingMethodException). Here's the code:

def getEntries(User user) {
 def entries = Entries.createCriteria().list() {

calendar { user { eq("user.id", user.id) } } } }

I have even tried the following variation:

def getEntries(User user) {
 def entries = Entries.createCriteria().list() {

calendar { eq("user", user) } } }

That did not raise an exception, but didn't work either.

Here's the relevant parts of the domain classes:

class Calendar {
    static belongsTo = [user: User]
    static hasMany = [entries: Entries]

    ...
}

class User {
    Calendar calendar

    ...
}

class Entry {
    static belongsTo = [calendar: Calendar]

    ...
}

When Googling I came across a similar problem noted in early 2008: http://jira.codehaus.org/browse/GRAILS-1412

But according to that link this issue should have been solved long ago.

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about grails

Related posts about gorm