Grails/GORM saves in the wrong order -
i using grails 1.3.6 , having problems getting saves cascade properly. problem classes have 2 parent classes. simplified version of attempting this:
class location { string city static hasmany = [authors: author, publishers: publisher] } class author { string name static belongsto = [location: location] static hasmany = [books: book] } class publisher { string name static belongsto = [location: location] static hasmany = [books: book] } class book { string title static belongsto = [author: author, publisher: publisher] } class srv1service { static transactional = true def loaddata() { def l1 = new location(city: "london") def a1 = new author(name: "graham greene") l1.addtoauthors(a1) def p1 = new publisher(name: "some press") l1.addtopublishers(p1) def b1 = new book(title: "the comedians") a1.addtobooks(b1) p1.addtobooks(b1) l1.save() } }
if run above loaddata, book instance saved before publisher instance, resulting in error "not-null property references null or transient value: adhoc.book.publisher".
i have tried various different ways of defining relationships little success. have tried interim saves, , work, can see parent tables updated save child data - ie location, author , publisher updated version 1. (and keep code simple can.) avoid linking tables.
any advice gratefully received!
okay, key here saves cascaded parent children. have problem when comes book because book child both publisher , author. gorm tries save location, location tries save author, author tries save book but save fails because book has transient publisher.
try adding intermediate save right before create book:
def loaddata() { def l1 = new location(city: "london") def a1 = new author(name: "graham greene") l1.addtoauthors(a1) def p1 = new publisher(name: "some press") l1.addtopublishers(p1) l1.save() // add save def b1 = new book(title: "the comedians") a1.addtobooks(b1) p1.addtobooks(b1) l1.save() }
i created local grails project domain classes adding in save. cascading working expect.
Comments
Post a Comment