(Quick Reference)

hasMany

Purpose

Defines a one-to-many association between two classes.

Examples

class Author {
    String name
    static hasMany = [books:Book]
}

In this example we define a one-to-many relationship between the Author class and the Book class (one Author has many books)

Description

By default GORM will create a property of type java.util.Set using the key inside the definition of the hasMany map. For example consider this definition:

static hasMany = [books:Book]

Here a property of type java.util.Set called books will be created within the defining class. These can then be iterated over and manipulated:

def a = Author.get(1)
a.books.each { println it.title }