Created
February 17, 2017 12:53
-
-
Save yvesf/8560eacf42e40ba0cced95b5951d9888 to your computer and use it in GitHub Desktop.
Grails/GORM cascade on save with SimpleMapSession
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.grails.datastore.gorm | |
import grails.gorm.tests.GormDatastoreSpec | |
import grails.persistence.Entity | |
import org.grails.datastore.mapping.dirty.checking.DirtyCheckable | |
class SaveCascadeSpec extends GormDatastoreSpec { | |
def "Test save() cascades on one-to-one"() { | |
given: "A domain model with a one-to-one" | |
def author = new OneToOneAuthor(name: 'AuthorsName') | |
def book = new OneToOneBook(name: 'BooksName') | |
author.favoriteBook = book | |
when: "Author is saved" | |
author.save(flush: true) | |
session.clear() | |
then: "Save cascades to favoriteBook" | |
author.favoriteBook.id != null | |
when: "The authors favorite books name changes" | |
author = OneToOneAuthor.findByName("AuthorsName") | |
author.favoriteBook.name = 'NewBooksName' | |
then: "The book is marked dirty" | |
((DirtyCheckable)author.favoriteBook).hasChanged() | |
when: "author is saved" | |
author.save(flush: true) | |
session.clear() | |
then: "The changed book name need to be persisted" | |
OneToOneBook.findByName('NewBooksName') != null | |
} | |
def "Test save() cascades on one-to-many"() { | |
given: "A domain model with a one-to-many" | |
def author = new OneToManyAuthor(name: 'AuthorsName') | |
def book = new OneToManyBook(name: 'BooksName') | |
author.addToFavoriteBookCollection(book) | |
when: "Author is saved" | |
author.save(flush: true) | |
session.clear() | |
then: "Save cascades to favoriteBookCollection" | |
author.favoriteBookCollection.first().id != null | |
when: "The authors favorite books name changes" | |
author = OneToManyAuthor.findByName("AuthorsName") | |
author.favoriteBookCollection.first().name = 'NewBooksName' | |
then: "The book is marked dirty" | |
((DirtyCheckable)author.favoriteBookCollection.first()).hasChanged() | |
when: "The author is saved" | |
author.save(flush: true) | |
session.clear() | |
then: "The changed book name need to be persisted" | |
OneToManyBook.findByName('NewBooksName') != null | |
} | |
@Override | |
List getDomainClasses() { | |
return [OneToOneAuthor, OneToOneBook, OneToManyAuthor, OneToManyBook] | |
} | |
} | |
@Entity | |
class OneToOneAuthor { | |
Long id | |
String name | |
OneToOneBook favoriteBook | |
} | |
@Entity | |
class OneToOneBook { | |
Long id | |
String name | |
static belongsTo = [author: OneToOneAuthor] | |
} | |
@Entity | |
class OneToManyAuthor { | |
Long id | |
String name | |
static mapping = { | |
favoriteBookCollection(lazy: true) | |
} | |
static hasMany = [favoriteBookCollection: OneToManyBook] | |
} | |
@Entity | |
class OneToManyBook { | |
Long id | |
String name | |
static belongsTo = [author: OneToManyAuthor] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment