Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 308
Mr.B Avatar asked Oct 29 '10 10:10

Mr.B


2 Answers

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

like image 168
Mr.B Avatar answered Oct 20 '22 15:10

Mr.B


def criteria = Msg.createCriteria()
results = criteria.list{
    user{
        eq("organisation", organisationInstance)
    }
}
like image 30
Aaron Saunders Avatar answered Oct 20 '22 16:10

Aaron Saunders