简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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