Announcement Announcement Module
Collapse
No announcement yet.
Can't create Criteria containing a belongsTo relation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't create Criteria containing a belongsTo relation

    Hi all,

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

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

    I'm trying to make the following query:

    Code:
    Msg.createCriteria.list() {
        ...
        user {
            eq("organisation", organisationInstance)
        }
        ...
    }
    All I'm getting is the following error

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

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

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

  • #2
    I suspect you have a variable or property that has the name 'user'. Can you post all the properties of the class in which this query is located as well as the method? I only need to see the method that is declaring the query - the others are irrelevant.

    Comment


    • #3
      Thanks pledbrook!!

      I've had this problem all over the place for some time and couldn't find any explanation why I couldn't use nested queries with belongsTo relation. The result from this was that I converted all my previous queries to HQL (that are less readable). When I think about it, all the errors I've been having that are related to criteriaBuilder and relations are those where I've have a variable named user inside the scope. I have been staring at the problem too close to see the error.

      Thanks again pledbrook! Order has been restored to the world, it is once again logical. :-)

      Comment

      Working...
      X