Search code examples
grailsgrails-ormgrails-2.0

Grails GORM: Updating an assoicated object's property on beforeUpdate not persisted


I have a domain class like:

class X {
    String name

    Y y 

    def beforeUpdate() {
        y.name = "new name"
        y.save() //DOESN'T work, adding flush: true throws exception
    }
}

Solution

  • The beforeUpdate event occurs during the flush, so you cannot trigger another flush. (If you do, you will get a stack overflow.)

    If the beforeUpdate returns false, it will cancel the save. I suggest you try the following:

    def beforeUpdate() {
        y.name = "new name"
        return true
    }
    

    I believe that the save method is returning null (failure), which is coerced to false.