Take the 2-minute tour ×

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):


class User {

String name 

static hasMany = [projectMemberships : ProjectMembership]

Project Membership

class ProjectMembership {

static constraints = {          

static belongsTo = [user:User, 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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

There are a handful of ways - here are a couple:

def user = User.get(userId)
ProjectMembership.findAllByUser(user).collect { it.project }

or to avoid the query for the User:

ProjectMembership.withCriteria {
    user {
        eq('id', userId)
}.collect { it.project }

Be wary of queries that'll return large result sets - you'll end up with a huge in-memory list of project objects.

share|improve this answer
thanks! worked perfect, just one small adjustment to your solution for reference for anyone else, findAllByUser took a user object as a param, but thanks, it was mainly the syntax and not knowing about the collect method –  andy mccullough Nov 28 '12 at 22:42
My bad on that, I had a meeting to run to and didn't put a lot of thought into it. I've updated the answer with a couple of options. –  Rob Hruska Nov 28 '12 at 23:02

Your Answer


