Criterion of sleep mode - selection of one object

I am working on a Grails project using Hibernate (GORM). I have the following domain models:

ClientContact {
   static hasMany = [owners: Person]
   static belongsTo = [Person]
}

Person {
   static hasMany = [clientContacts: ClientContact]
}

When I try to get everything ClientContactswith a specific owner ( Person), I run into some funny problems. I use the following query criteria:

def query = {
   owners {
      eq("id", Long.parseLong(params.ownerId))
   }
}
def criteria = ClientContact.createCriteria()
def results = criteria.list(params, query) 

The problem is that when I repeat each of mine ClientContactsin the results, they have only one owner - when in fact most have many other owners. What gives? I know that hibernate / GORM uses a lazy choice, but I thought that he would come all the other owners on ClientContactwhen I try to access them.

? list(), .

+3
3

, , , , :

def query = {
        owners {
                eq("id", Long.parseLong(params.ownerId))
        }
}

:

def query = {
        createAlias("owners", "o")
        eq("o.id", Long.parseLong(params.ownerId))
}

, . . : http://adhockery.blogspot.com/2009/06/querying-by-association-redux.html

+3

id and version are special properties of all GORM classes. You do not need to specify them in the class declaration, and you cannot use the standard criteria with them.

You definitely need to use the eqID criterion

   def query = {
          owners {
                eqId(Long.parseLong(params.ownerId))
          }
   }
   def criteria = ClientContact.createCriteria()
   def results = criteria.list(params, query)
0
source

Source: https://habr.com/ru/post/1698996/


All Articles