每当在当前表中更改字段时,我都必须将旧数据保存到历史表中。因此,我必须创建一个与原始Domain类具有相同字段的历史Domain类。现在,我将手动创建历史记录Domain类,并在原始表中更新值时将较旧的数据保存到其中。
每当创建新的Domain类时,是否有一种方法可以自动生成具有相同字段的历史Domain类。
主域类是:
class Unit {
String name
String description
Short bedrooms = 1
Short bathrooms = 1
Short kitchens = 1
Short balconies = 0
Short floor = 1
Double area = 0.0D
Date expDate
Date lastUpdated
static hasMany = [tenants:Tenant]
static belongsTo = [property: Property]
}
History Domain类应如下所示:
class UnitHistory {
String name
String description
Short bedrooms = 1
Short bathrooms = 1
Short kitchens = 1
Short balconies = 0
Short floor = 1
Double area = 0.0D
Date expDate
Date lastUpdated
static hasMany = [tenants:Tenant]
static belongsTo = [property: Property]
}
最佳答案
也许您可以将beforeInsert
和beforeUpdate
方法添加到Unit
域中,如下所示:
class Unit {
String name
String description
Short bedrooms = 1
Short bathrooms = 1
Short kitchens = 1
Short balconies = 0
Short floor = 1
Double area = 0.0D
Date expDate
Date lastUpdated
def beforeInsert() {
addHistory()
}
def beforeUpdate() {
addHistory()
}
def addHistory(){
new UnitHistory( this.properties ).save( failOnError: true )
}
}