Search code examples
mongodbgrailsgrails-orminterceptor

Fields changed in beforeUpdate not persisted


In my Grails 3.1.14 app I have a domain class:

class MyDomain {

  String text

  def beforeInsert() { beforeUpdate() }

  def beforeUpdate() {
    text = changeTextBasedOnOtherFields()
    println ">>> $text"
    true
  }
}

in a controller the instance gets saved with myDomain.save flush:true.

The problem is, that the text property is ignored by saving, although I can see the println's output with a proper value in a console.

If I put the text changing code just in front of saving:

myDomain.text = changeTextBasedOnOtherFields()
myDomain.save flush:true

then it works like charm!

Any way to solve the mistery?


Solution

  • It appears to be a GORM-gotcha.

    Instead of using a property in interceptor-methods, one should use a setter:

    def beforeUpdate() {
      setText changeTextBasedOnOtherFields()
      true
    }