domain中设置了dateCreated或lastUpdated,Grails就会在创建记录和更新记录的时候,自动更新这两个字段。可在mapping中设置autoTimeStamp(false)关闭自动设置。
注意:当在mapping中设置了autoTimeStamp(false)后,domain不能使用构造方法为dateCreated或lastUpdated设置值,必须使用对象的set方法设置才能成功!!!
如:
class Twitter {
String content
Date dateCreated
Date lastUpdated
static constraints = {
content(nullable: false,blank: false)
}
static mapping = {
version(false)
autoTimestamp(false)
}
}
保存Twitter对象时,必须如下设置值才有效:
Twitter twitter = new Twitter(content: params.content)
twitter.dateCreated=new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').parse(params.time)
twitter.lastUpdated=new Timestamp(System.currentTimeMillis())
twitter.save flush: true