tags:

views:

189

answers:

1

I'm having trouble figuring out how to do 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])

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?

+2  A: 

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])
Burt Beckwith
Ok! I see. When you need to go to HQL, the dymanic function names like "findAll" have no real meaning and it's just an ExecuteQuery....
ראובן
Essentially, yes. I've never understood why there isn't a "Domain" static class that has these functions instead of placing them on all domain classes.
Bill James