Grails many to many using a third 'join' class

Posted by andy mccullough on Stack Overflow See other posts from Stack Overflow or by andy mccullough
Published on 2012-11-28T22:25:37Z Indexed on 2012/11/28 23:04 UTC
Read the original article Hit count: 175

Filed under:
|

I read that a m:m relationship often means there is a third class that isn't yet required. So I have m:m on User and Project, and I created a third domain class, ProjectMembership

The three domains are as follows (minimized for illustration purposes):

User

class User {

String name 

static hasMany = [projectMemberships : ProjectMembership]
}

Project Membership

class ProjectMembership {

static constraints = {          
}

static belongsTo = [user:User, project:Project]
}

Project:

class Project {

String name

static hasMany = [projectMemberships : ProjectMembership]

     static constraints = { 
     }
}

If I have the ID of the user, how can I get a list of Project objects that they are assigned to?

© Stack Overflow or respective owner

Related posts about grails

Related posts about many-to-many