Search code examples
grailsentity-relationship

Overcoming "No owner defined between domain classes" in Grails many-to-many relationship


I was following the "Grails in Action" book in order to create the following scenario.

A map belongs to one or many mapsets. A mapset can contain one or many maps. Both belong to a user.

So, what I did was:

Map.groovy:

static hasMany = [ sets : Mapset ]
static belongsTo = [ user : User, set : Mapset ]

Mapset.groovy:

static hasMany = [ maps : Map ]
static belongsTo = [ user : User ]

User.groovy:

static hasMany = [ maps : Map, sets: Mapset ]

Still, I receive this error when running the app:

Caused by: org.codehaus.groovy.grails.exceptions.GrailsDomainException: No owner defined between domain classes [class at.package.Mapset] and [class at.package.Map] in a many-to-many relationship. Example: static belongsTo = at.package.Map

It doesn't change anything if I write the fully qualified class name for Map.


Solution

  • Whoops, this is weird. The following the trick:

    In Map.groovy:

    static belongsTo = [ User, Mapset ]
    

    I'd probably keep this as an answer instead of deleting the question because there's no reference for this error message on SO yet