【问题标题】:Using ZonedDateTime with Grails 3 lastUpdated and dateCreated fields将 ZonedDateTime 与 Grails 3 lastUpdated 和 dateCreated 字段一起使用
【发布时间】:2023-03-11 22:57:01
【问题描述】:

Grails 3 是否支持对 dateCreated 和 lastUpdated 字段使用 Date 以外的任何类,例如 ZonedDateTime?例如,

...
ZonedDateTime dateCreated
ZonedDateTime lastUpdated
...

我知道 MySQL 支持 ZonedDateTime,所以我很好奇 GORM 是否支持它。

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    是的,确实如此。由于 Grails 3 仍然支持 Java 7,因此您需要在 build.gradle 中添加一些依赖项以添加对它的支持:

    // Hibernate java 8 java.time support 
    compile "org.hibernate:hibernate-java8:$hibernateVersion" 
    // Grails java 8 java.time support
    compile "org.grails.plugins:grails-java8:1.2.3"
    

    这里有很多使用 java.time 和 Grails 的好技巧:https://giri-tech.blogspot.com/2018/01/add-time-zone-sense.html

    【讨论】:

      【解决方案2】:

      本机,没有。但是,如果您愿意,您自己使用这些将是非常微不足道的。您需要先关闭域对象的自动时间戳:

      static mapping = {
          autoTimestamp false
      }
      

      然后使用事件(beforeUpdate、beforeInsert)来填充:

      def beforeUpdate() {
          lastUpdated = // whatever you want it to be
      } 
      

      【讨论】:

        猜你喜欢
        • 2012-02-21
        • 2012-11-11
        • 1970-01-01
        • 2015-04-24
        • 1970-01-01
        • 2012-06-28
        • 1970-01-01
        • 1970-01-01
        • 2015-01-25
        相关资源
        最近更新 更多