我有一个要求,每当更新数据域类时,旧值应该以JSON格式存储到数据库表中,以便随后可以随时对其进行审计。如何强制gorm给数据库对象不是缓存实例?
这里我有三类:
数据 - 超类
A - 子类(还有其他子类也是如此)
历史 - 表,需要保存旧数据
class Data implements Serializable{
Date dateCreated
Date lastUpdated
User createdBy
User updatedBy
Status status
static mapping = { tablePerHierarchy false }
def beforeUpdate() {
def jsonData = (Data.get(this) as JSON).toString()
new History(data:jsonData, domain: this.class.toString(), refid: this.id).save()
}
}
class User extends Data{
String name
static constraints = {
}
}
class History {
String data
Date dateCreated
String domain
Date lastUpdated
long refid
static constraints = {
}
}
数据域类中的beforeUpdate()方法获取当前值作为json不是旧值(数据库中存在)
def jsonD ata =(Data.get(this)as JSON).toString()
我正在寻找某种方法,它将获取数据库中当前值的整个对象。
已尝试过.refresh()方法吗? – elixir