这个问题与Grails 2.1.1有关。我有一个域对象,其中包含3个在控制器中设置的日期字段。我有一个错误,在控制器中设置的日期被忽略,替换为当前日期
奇怪的是,只有当我将域对象中的相应字段重命名为具有“日期”后缀时,才会解决此问题。
我想知道:
分析和代码示例如下:
原始域对象和控制器 - 在这种情况下,为“dateCreated”和“lastUpdated”设置的值将被系统日期忽略并覆盖。
class User {
String name
String title
String firstName
String lastName
Date companyCreationDate
Date dateCreated //works when renamed to createdDate (and controller updated accordingly)
Date lastUpdated //works when renamed to lastUpdatedDate (and controller updated accordingly)
static mapping = {
id column:'record_id'
}
static constraints = {
id()
title(blank: false, maxSize: 35)
firstName(blank: false, minSize: 1, maxSize: 35)
lastName(blank: false, minSize: 1, maxSize: 35)
}
}
class UserController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
static df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)
def save() {
def user = new User()
user.name = params.name
user.companyCreationDate = df.parse("2006-09-07 13:26:15");
user.title = params._title
user.firstName = params._fname
user.lastName = params._lName
user.creationDate = df.parse("2006-09-07 00:00:00");
user.lastUpdatedDate = df.parse("2006-09-07 00:00:00");
if (user.validate()){
user.clearErrors();
if (user.save(flush: true)) {
flash.message = message(code: 'default.created.message', args: [message(code: 'registration.label', default: 'Registration'), user.id])
redirect(action: "show", id: user.id)
}
}
else
{
render(view: "create", model: [userInstance: user])
}
在Hibernate中设置跟踪选项之后,我注意到在保存域对象(即调用'save(flush:true)'和Hibernate执行持久性操作之间)发生了这种转换。