Grails 3和Java 8时间支持 [英] Grails 3 and Java 8 time support

查看:113
本文介绍了Grails 3和Java 8时间支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在grails 3应用程序中,我看到新的java.time类被保存到数据库中。动态查询和创建标准的作品。但是,我想知道是否有更好的方法来将这些时间数据存储到数据库中。当我查看数据库时,我看到一些二进制数据,它似乎只是序列化时间对象。



是否有任何与joda-time-plugin类似的插件,或者是否有任何类似的插件如何为java.time类配置数据库映射?

解决方案


strong>:Grails 3.1.1有hibernate 5,所以这不再是一个问题了......


Grails



我终于找到了解决方案。首先,hibernate 5为java 8时间实现持久化( https://hibernate.atlassian.net/browse / HHH-8844 )。然而,grails 3使用hibernate 4,我还没有发现任何计划在grails中升级hibernate。



有一个java 8时间hibernate用户类型的实现(jadira extended包),它可以像joda时间插件一样使用(请参阅 http:// gpc .github.io /乔达时间/导向/ persistence.html )。对我而言,只有版本3.1.0.GA和更低的工作。



build.gradle

  dependencies {
compileorg.jadira.usertype:usertype.extended:3.1.0.GA
}

application.groovy

  grails.gorm.default.mapping = {
user-typetype:org.jadira.usertype.dateandtime.threeten.PersistentInstantAsMillisLong,class:java.time.Instant
}


In grails 3 application, I see that new java.time classes are persisted to database. Dynamic queries and create criteria works. However, I am wondering if there is some better way how to store these time data to database. When I look to database I see some binary data, it seems it just serialize the time objects.

Is there any similar plugin as joda-time-plugin or is there any way how to configure database mapping for java.time classes?

解决方案

Edit: Grails 3.1.1 has hibernate 5 so this is not an issue anymore...

Grails

I finally found a solution. First of all hibernate 5 implements persistent for java 8 time (https://hibernate.atlassian.net/browse/HHH-8844). However, grails 3 uses hibernate 4 and I haven't found any plans to upgrade hibernate in grails.

There is an implementation for java 8 time hibernate user types (jadira extended package) which can be used similarly as in joda time plugin (see http://gpc.github.io/joda-time/guide/persistence.html). For me only version 3.1.0.GA and lower worked.

build.gradle

dependencies {
    compile "org.jadira.usertype:usertype.extended:3.1.0.GA"
}

application.groovy

grails.gorm.default.mapping = {
    "user-type" type: org.jadira.usertype.dateandtime.threeten.PersistentInstantAsMillisLong, class: java.time.Instant
}

这篇关于Grails 3和Java 8时间支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆