Last updated by admin 4 years ago
???????? Managing Relationships
Since 0.3, Grails provides quite a few methods to easy relationship management. Firstly if you have a one-to-many relationship such as the Author-Book relationship defined below:class Author {
String name
static hasMany = [books:Book]
}
class Book {
String title
Author author
static belongsTo = Author
}def a = new Author(name:"Stephen King") .addBook(new Book(title:"IT")) .addBook(new Book(title:"The Stand")) .save()
class Author {
String name
static hasMany = [fiction:Book, non-fiction:Book]
}def a = new Author(name:"Stephen King") .add(to:"fiction", new Book(title:"IT")) .add(to:"non-fiction", new Book(title:"On Writing: A Memoir of the Craft")) .save()



