Pavel Vlasov
Pavel Vlasov

Reputation: 4331

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()

Upvotes: 1

Views: 326

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

You can use isDirty to make this check.

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

Upvotes: 3

Related Questions