Grails 3和Java 8时间支持 [英] Grails 3 and Java 8 time support
问题描述
是否有任何与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屋!