简体   繁体   English

日期字段的Grails GORM域对象命名约定和持久性后果

[英]Grails GORM domain object naming conventions for date fields, and persistence consequences

This question pertains to Grails 2.1.1. 这个问题与Grails 2.1.1有关。 I have a domain object which contains 3 date fields which are set in the controller. 我有一个域对象,其中包含在控制器中设置的3个日期字段。 I had a bug where the date being set in the controller was ignored and replaced with the current date 我有一个错误,其中控制器中设置的日期被忽略,并替换为当前日期

Oddly, this behaviour was only resolved when I renamed the corresponding fields in the domain object to have a 'date' suffix. 奇怪的是,仅当我将域对象中的相应字段重命名为具有“ date”后缀时,此行为才得以解决。

I would like to know: 我想知道:

  • if there is a particular naming convention that needs to be followed, 如果需要遵循特定的命名约定,
  • or whether there is a different underpinning issue I haven't understood (I know there is quite a bit of grails 'magic' that happens under the hood which frankly I am not too comfortable with) 或者是否存在我不了解的其他基础问题(我知道在引擎盖下会发生很多grails的“魔术”,坦率地说,我不太满意)

The analysis and code samples are provided below: 下面提供了分析和代码示例:

Original domain object and controller - in this instance, the the values set for the 'dateCreated' and 'lastUpdated' are ignored and overridden by the system date. 原始域对象和控制器-在这种情况下,为“ 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 { 类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])
    }

After setting the tracing options in Hibernate, I noticed that this transformation was happening between saving the domain object (ie calling 'save(flush: true)' and Hibernate performing the persistence action. 在Hibernate中设置跟踪选项之后,我注意到在保存域对象(即调用“ save(flush:true)”)和Hibernate执行持久性操作之间发生了这种转换。

休眠跟踪输出

您可以通过将autoTimestamp设置为false来禁用此功能: Grails Doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM