Search code examples
grailsgrails-orm

Errors trying to search a many to many relationship in grails


I'm having trouble contructing a query within an many to many relationship... I have these domain classes:

class Event {

Appuser creator
static belongsTo = Appuser
static hasMany = [guests: Appuser]

and

class Appuser {


static hasMany = [friends: Appuser, events: Event]

So the idea is a user can have friends, and they can set up and own multiple events, and they can also be guests of other user's events.

My issue is constructing a query to get the list of guests for a particular event...

I've tried in my controller:

def guests = Appuser.findAllByEvent(eventInstance)

this gives an error

No property found for name [event] for class

def guests = Appuser.findAllByEvents(eventInstance)

this gives an error

No value specified for parameter 1

Any ideas how to remedy? Thanks.


Solution

  • Appuser.findAllByEvent doesn't make sense because there's no event property in the class. Dynamic finders can only work with persistent properties. findAllByEvents is more likely to work because there is an events property (added by an AST transform because of the hasMany) but you can't query on collections with dynamic finders; you need to use criteria/where/HQL queries for those.

    But you don't need a query at all - just use the hasMany property you declared:

    Event eventInstance = ...
    def guests = eventInstance.guests