Creating many-to-many relationships in Grails is quite simple. It is also possible to have multiple many-to-many relationships in a single class, but for some reason I found little documentation on how to do this. It is actually quite simple once you know how to and pretty much comes down to syntactic sugar, so to speak. Read more after the break.

Let’s assume you wanted to model two distinct domain classes which both can have Tags.

Many-to-Many UML Diagram

Now, the owning side of the relationship should be the DomainClass1 and DomainClass2 respectively.

Your domain class code would look something like this:

class DomainClass1 {
  ...
  static hasMany = [
    tags: Tag
  ]
  static mappedBy = [
    tags: "domainClass1s"
  ]
}

class DomainClass2 {
  ...
  static hasMany = [
    tags: Tag
  ]
  static mappedBy = [
    tags: "domainClass2s"
  ]
}

Next, you need to define the Tag class:

class Tag {

  String value

  static hasMany = [
    domainClass1s: DomainClass1,
    domainClass2s: DomainClass2
  ]

  static mappedBy = [
    domainClass1s: "tags",
    domainClass2s: "tags"
  ]

  static belongsTo = [
    DomainClass1, DomainClass1
  ]

  static constraints = {
    value(unique:true)
  }

  @Override public String toString() {
    return value
  }
}

The part that is rarely documented online is the belongsTo statement.

static belongsTo = [
  DomainClass1, DomainClass1
]

This tells groovy the owners are DomainClass1 and DomainClass2. You’re done. Pretty simple, isn’t it? Hope this helps someone.

References

http://www.grails.org/doc/latest/guide/single.html#5.5.2%20Custom%20ORM%20Mapping

http://osdir.com/ml/lang.groovy.grails.user/2008-05/msg02094.html

Leave a comment