简体   繁体   English

我如何知道Grails域POJO自检索后是否发生了变化?

[英]How do I know if Grails domain POJO was changed since its retrieval?

I am patching a domain object in my code. 我在我的代码中修补域对象。 At the end, I need to save the object but only if it has been actually changed. 最后,我需要保存对象,但只有在实际更改后才能保存。 Is it possible to avoid custom boolean flags having code like that? 是否有可能避免使用具有类似代码的自定义布尔标志?

User user = User.find(...)
if(maybe)
  user.name = "John"
if(user.changed())
  user.save()

You can use isDirty to make this check. 您可以使用isDirty进行此检查。

if(user.isDirty() && user.save()) {
  // user saved successfully
}

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

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