discard
does not return the instance itself. It returns nothing (void) but evicts the object getting persisted in future. Use it as:
def oldInstance = User.get(id)
oldInstance.discard()
On a side note, if the sole reason is to compare the old and new values of the properties in the instance then you can use dirtyPropertyNames
and getPersistentValue()
before flushing the instance as below:
userInstance.properties = params
userInstance.dirtyPropertyNames?.each { name ->
def originalValue = userInstance.getPersistentValue( name )
def newValue = userInstance.name
}
//Or groovier way
userInstance.dirtyPropertyNames?.collect {
[
(it) : [oldValue: userInstance.getPersistentValue( it ),
newValue: userInstance.it]
]
}