(Quick Reference)
hasMany
The
hasMany
property declares one-to-many and many-to-many relationships between objects. For example, the class:
class Author
{
String name static hasMany = [books: Book]
}
defines a relationship where each Author has zero or more Books. You iterate over the list of books an author has
by referencing the name of the property:
author.books.each {book ->
println book.title
}
The default type returned from this query is an ordered Set. The order of the elements is determined by the natural
order of the primary keys of the referenced class (Book, in this case). If you'd prefer the type to be a List, so that
you can use the index operator, just declare a List parameter with the same name:
class Author
{
String name
List books
static hasMany = [books: Book]
}
Has Many relationships are always lazy-evaluated (i.e. not read from Cassandra until the property is mentioned). There
is also an built-in limit of 5000 items.