Skip to content

Instantly share code, notes, and snippets.

@fxg42
Created April 2, 2014 13:20
Show Gist options
  • Select an option

  • Save fxg42/9933940 to your computer and use it in GitHub Desktop.

Select an option

Save fxg42/9933940 to your computer and use it in GitHub Desktop.
gotchas...

GOTCHAS!

Notes

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.

many-to-one


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 delete the 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 delete the 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 }

one-to-many

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

many-to-many

Mapping lookup data

Fooling around with equals and hashcode

@EqualsAndHashCode
class Location { String description }
  • Creating 2 (equal) instances adn saving them stores 2 rows

    @EqualsAndHashCode(includes="description") class Location { String description }

Playing with eager vs lazy

  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
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment