Search code examples
grailsgrails-ormcriteria

Left Join grails criteria


we have a User class

class User {
...
}

and another class:

class Licence {

    User user
    Event event

}

What we want to do is the Left Join (because not for every user we have an entry in the licence class).

We naively did this:

def a = User.withCriteria {

    createAlias("Licence", "l", CriteriaSpecification.LEFT_JOIN)
    eq("id", "l.user.id")

    if (something == true)
        isNull("l.id")
    else
        isNotNull("l.id")
}

But the query fails with:

could not resolve property Licence of User

Can somebody help how to do this query?


Solution

  • The problem is that grails is not aware of the connection on the User side if you do not have a foreign key or at least hasOne. If you had static hasOne = [licence: Licence] your code would work as answered here.