Search code examples
hibernategrailsgrails-orm

Grails GORM beforeUpdate method not invoked


I'm trying to fix an issue with my application where the beforeUpdate method in one of my domain classes is not invoked.

If none of the fields belonging directly to the domain class are updated, then the beforeUpdate event isn't fired. I guess this makes sense, however I would like the code in my beforeUpdate method to execute even in cases where only associations of my domain class are updated.

For instance, my domain class looks something like this:

class Organisation {
    String name
    Location location

    def beforeUpdate() {
        //do some stuff with the location when it's updated
    }
}

If the parameters map submitted from the form contains the name parameter, then beforeUpdate is fired. If the parameters map only contains location parameters, then it is not.

Is there any way to make Grails fire beforeUpdate even if I'm only updating associations?


Solution

  • The short answer to your question is:

    beforeUpdate will not fire unless one of the values of the properties on the domain class has changed.

    In your case beforeUpdate is not being called because no property of Organisation has changed.

    The only thing I can think to do in this situation is to change your domain model to allow Location to be aware of it's owning Organisation and manually call beforeUpdate from within the Location's beforeUpdate. All though, this feels hacky and smells bad.