class B { static belongsTo = A } defines the cascading behaviour from A to B.
When domain B belongsTo A, saves and deletes from A cascade to B.
Operations are cascaded from A to B but not from B to A.
class A { static hasMany = [bs:B] } defines a one-to-many relationship, a
collection and a cascading behaviour from A to B.
class Author {
String name
Location location
}
class Location {
String description
}
- Unidirectional
- Mapped with a foreign key
create table author(location_id bigint not null) - Inserts are not cascaded
- Dirty-checking still applies (
Author.get(2).location.description = 'other') - Can't
deletethe location - Deletes are not cascaded
class Author {
String name
Location location
}
class Location {
String description
static belongsTo = Author
}
- Unidirectional
- Mapped with a foreign key
create table author(location_id bigint not null) - Inserts are cascaded
- Dirty-checking still applies (
Author.get(2).location.description = 'other') - Can't
deletethe location - Deletes are cascaded
- When many Authors for one Location, deleting an Author deletes the author row but throws an exception when trying to delete Location row... (why did the first delete succeed?)
class Author {
String name
Location location
}
class Location {
String description
static belongsTo = [ author: Author ]
}
- Does not work properly. When trying to fetch Location, tries to find matching
author, finds more than one and fails... Author needs
static constraint = { location unique: true }
class Author {
String name
static hasMany = [ books: Book ]
}
class Book {
String title
}
- Unidirectional
- Mapped with a join table
- Saves on Author are cascaded to Books.
- Clearing the book collection does not delete Books
- Deleting an Author does not delete related Books
class Author {
String name
static hasMany = [ books: Book ]
}
class Book {
String title
static belongsTo = Author
}
- Unidirectional
- Mapped with a join table
- Saves on Author are cascaded to Books.
- Clearing the book collection does not delete Books
- Deleting an Author deletes all related Books
class Author {
String name
static hasMany = [ books: Book ]
}
class Book {
String title
static belongsTo = [ author: Author ]
}
- Bidirectional
- Mapped with a foreign key
create table book(author_id bigint not null) - Saves on Author are cascaded to Books.
- Clearing the book collection does not delete Books
- Deleting an Author deletes all related Books
class Author {
String name
static hasMany = [ books: Book ]
static mapping = {
books cascade: 'all-delete-orphan'
}
}
class Book {
String title
}
- Unidirectional
- Mapped with a join table
- Saves on Author are cascaded to Books.
- Clearing the book collection deletes Books
- Deleting an Author deletes all related Books
class Author {
String name
static hasMany = [ books: Book ]
static mapping = {
books cascade: 'all-delete-orphan'
}
}
class Book {
String title
static belongsTo = Author
}
- Unidirectional
- Mapped with a join table
- Saves on Author are cascaded to Books.
- Clearing the book collection deletes Books
- Deleting an Author deletes all related Books
class Author {
String name
static hasMany = [ books: Book ]
static mapping = {
books cascade: 'all-delete-orphan'
}
}
class Book {
String title
static belongsTo = [ author: Author ]
}
- Bidirectional
- Mapped with a foreign key
create table book(author_id bigint not null) - Saves on Author are cascaded to Books.
- Clearing the book collection deletes Books
- Deleting an Author deletes all related Books
@EqualsAndHashCode
class Location { String description }
-
Creating 2 (equal) instances adn saving them stores 2 rows
@EqualsAndHashCode(includes="description") class Location { String description }
import gotchas.*
//def a = new Author(name:'name')
//100.times { a.addToBooks(new Book(title:'title')) }
//a.save(flush:true)
//def all = Author.list()
def all = Author.list(fetch:[books:'eager'])
all.each { a ->
a.books.each { b ->
print b.title
}
}