Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails domain class property change is not flagged as Dirty

I have a transient method inside of my domain class that will update a property of the class. When I use this method the class is not marked as dirty and does not save.

class Major {
    String code
    String major

    static transients = ['update']

    def update(String newVal) {
        major = newVal
    }
}

Major major = Major.findByCode("ACAA");
major.update("NEW VALUE");
println("Is dirty? "+ major.dirty);  //Is dirty? false

When I update the property outside the method it works as expected and I can save

Major major = Major.findByCode("ACAA");
major.major = "NEW VALUE";
println("Is dirty? "+ major.dirty);  //Is dirty? true

Is there a reason this does not work?

Grails 3.3.1

GORM 6.1.6

like image 579
Raymond Holguin Avatar asked Dec 19 '22 00:12

Raymond Holguin


1 Answers

The error lies in the update function. It needs to explicitly call the setter like this:

def update(String newVal) {
    setMajor(newVal)
}

For reference, see the GORM upgrade notes for the new dirty checking implementation.

like image 63
MW. Avatar answered Dec 29 '22 10:12

MW.