Search code examples
grailscriteriagrails-ormhibernate-criteria

Can't create grails Criteria query containing a belongsTo relation


I've been trying to create a criteria builder containing a belongsTo relation and have yet to succeed. Consider the following model:

class Msg {
    ...
    static belongsTo = [user: User]
    ...
}  

class User {
    ...
    Organisation organisation
    ...
}  

I'm trying to make the following query:

Msg.createCriteria().list() {
    ...
    user {
        eq("organisation", organisationInstance)
    }
    ...
}

All I'm getting is the following error

ERROR errors.GrailsExceptionResolver  - No signature of method: static User.call() is applicable for argument types: (MsgService$_findMessages_closure1_closure6) values: [MsgService$_findMessages_closure1_closure6@afcba8]
Possible solutions: save(), wait(), any(), getAll(), save(java.lang.Boolean), save(java.util.Map)

I've tried to add different small additions to the criteria query like:

join "user"
fetchMode("user", org.hibernate.FetchMode.EAGER)

But still get the same problem.

I even tried to add the following static mapping to the Msg class:

static mapping = {
    columns {
        user lazy: false
    }
}

Still not working.

Is there a way to use criteria builder containing a belongsTo query at all?

Thanks for your help in advance.
Lucien


Solution

  • I've found the solution! The problem wasn't related to criteria builder at all. In this instance, I had a user variable inside the scope. Once a removed it it works like a charm :-)