When I change a domain object, rather than updating the database record, I’d like to archive the old record record and create a new one. I’d like to force GORM to do this automatically.
For example, if I had this domain class:
class User {
String username
String email
Boolean active
}
then I’d like
user.email = email
user.save()
to set the active flag on the user to false, keeping a record of the old email. A new record with the new email address and active = true would be inserted.
Is this a common pattern? Is it easy to do in the domain class so that it happens transparently?
You can use GORM event
beforeUpdateinto User domain class (see the documentation here and here) with something like the following should work (not tested) :