Learn Java for Web Development

(Tina Meador) #1
CHAPTER 7: Rapid Web Development with Grails 347

Creating Domain Relationships


In an application domain, classes have relationships to one another. The domain relationships define
how domain classes interact. Grails provides support for several types of relationships between domain
classes. A one-to-many relationship is when one class, Author, has many instances of the Book class.
With Grails you define such a relationship with the hasMany setting, as illustrated in Listing 7-31.


Listing 7-31. One-to-Many Relationship Between Author and Book


class Author {
static hasMany = [books: Book]


String name }


class Book {
String title
}


Figure 7-31. Querying the table in H2

Free download pdf