开发者

Why does this Grails/HQL query with a JOIN return Lists of pairs of domain classes?

I'm having trouble figuring out how to do a "join" in Groovy/Grails and the return value开发者_Python百科s 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])

latestPhotosForUser isn't a list of PhotoOwners. It's a list of [PhotoOwner, PhotoStorage] pairs. Since I'm doing a PhotoOwner.findAll, I would have expected to see only PhotoOwners.

Am I doing something wrong, or is this the proper behavior?


executeQuery and findAll are a little misleading since the class that you call them on have no bearing on the query or its return type - GORM adds the methods to all domain classes.

Given the way you asked the question I'm assuming you want

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


I would say that you can achieve this by a dynamic finder, something like this:

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

The you can just dig down into the latestPhotosForUser.photo in order to get to the storage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜