One-to-Many Relationships

A TekEvent will have one organizer but will need more than one volunteer to be successful. A volunteer is also a TekUser, and we just set up a relationship between TekEvent and TekUser. We’re going to set up another relationship between these two classes, but this time it will be a one-to-many relationship. A TekEvent will have zero or more volunteers.

Grails uses a static property called hasMany to declare one-to-many relationships. hasMany is a Map, with the key being the name of the collection in the owning class and the value being the type of the child class. Let’s see how that looks in our TekEvent. Open TekDays/grails-app/domain/com/tekdays/TekEvent.groovy, and add the hasMany declaration, as shown in the following ...

Get Grails 2: A Quick-Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.