简体   繁体   English

Grails域类的属性更改未标记为“脏”

[英]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 Grails 3.3.1

GORM 6.1.6 GORM 6.1.6

The error lies in the update function. 错误在于更新功能。 It needs to explicitly call the setter like this: 它需要像这样显式调用setter:

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

For reference, see the GORM upgrade notes for the new dirty checking implementation . 作为参考,请参阅GORM升级说明以了解新的脏检查实现

That method does not need to be marked as transient. 该方法无需标记为瞬态。 transient properties are usually used if you have a method named getThing() so it's not interpreted as the property thing that would need to be persisted. transient特性通常使用,如果你有一个名为方法getThing()所以它不能解释为财产thing ,将需要被保留。

Just remove the field from the transients list 只需从transients列表中删除该字段

http://docs.grails.org/3.3.1/ref/Domain%20Classes/transients.html http://docs.grails.org/3.3.1/ref/Domain%20Classes/transients.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM