2
votes

I am trying to change a property of the domain object inside the beforeUpdate event. The problem is that the changed property does not reach the database. The domain class in question has the dymanicUpdate set to true. I noticed that if I change the dynamicUpdate to false the property gets persisted to the DB.

I created a simple Grails 3.2.9 project with GORM 6.0.11 with a single domain class.

class Example {

    Integer status
    Date dateCreated
    Date dateClosed
    Date lastUpdated

    static constraints = {
        status nullable: true
        dateClosed nullable: true
    }

    static mapping = {
        dynamicUpdate true
    }

    def beforeUpdate() {
        if (isDirty('status')) {
             dateClosed = new Date();
        }
    }
}

By default dynamicUpdate is set to false and the property I change in the beforeUpdate event gets persisted to the database. Although, if I switch the dynamicUpdate to true, the property changed in the beforeUpdate event is not persisted anymore to the database. Instead of the current date I get a null in the dateCreated column.

Any idea what is the reason for this behaviour and how to get the same result with the dynamicUpdate set to true?

1

1 Answers

0
votes

I'm not sure if it's related to dynamicUpdate, but I found out recently, that in GORM's before* interceptors instead of java bean notation you should be using setters.

So this

def beforeUpdate() {
  dateClosed = new Date()
}

should be

def beforeUpdate() {
  setDateClosed new Date()
}

The reason for that behavior is that the property you are changing is not marked as dirty if done within the same class.