Why is this Grails / HQL query returning JOIN Lists of domain class pairs?

It’s hard for me to figure out how to make a “join” in Groovy / Grails and the return values ​​I get

person = User.get(user.id)
def latestPhotosForUser = PhotoOwner.findAll(
  "FROM PhotoOwner AS a, PhotoStorage AS b WHERE (a.owner=:person AND a.photo = b)", 
  [person:person], [max:3])

lastPhotosForUser is not a list of PhotoOwners. This is a list of [PhotoOwner, PhotoStorage] pairs. Since I am doing PhotoOwner.findAll, I would expect to see only PhotoOwners.

Am I doing something wrong or is this the right behavior?

+3
source share
2 answers

executeQueryand a findAlllittle misleading, since the class that you call it is not related to the request or its return type - GORM adds methods to all classes in the domain.

, , ,

def latestPhotosForUser = PhotoOwner.executeQuery(
   "SELECT b FROM PhotoOwner a, PhotoStorage b WHERE a.owner=:person AND a.photo = b",
   [person:person], [max:3])
+5

, , :

def latestPhotosForUser = PhotoOwner.findAllByOwner(person, [max:3])

lastPhotosForUser.photo, .

0

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


All Articles