使用 JPA2/Hibernate 持久化 java.time.Instant (JDK8) [英] Persist java.time.Instant (JDK8) with JPA2/Hibernate

查看:31
本文介绍了使用 JPA2/Hibernate 持久化 java.time.Instant (JDK8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA 和 Hibernate 目前都不支持 JDK8 中 JSR-310 带来的新日期/时间类(JPA ticket、休眠).尽管如此,我还是想使用 JDK8 日期/时间类进行编码,因为它们最终设计得很好.特别是,我对 java.time.Instant 感兴趣,而不是完全支持所有 java.time.* 类型,因为我所有的实体都将使用这个特定的类(或者我现在认为,至少:-)

Neither JPA nor Hibernate currently support the new date/time classes brought by JSR-310 in JDK8 (JPA ticket, Hibernate ticket). Nonetheless, I'd like to code with the JDK8 date/time classes as they are finally well designed. In particular, I'm interested in java.time.Instant, not in full support for all java.time.* types, as all my entities will use this particular class (or so I think now, at least :-)

一种选择是编写一个 类型转换器,由 JPA 2.1 定义.然而,我们的应用服务器是 JBoss EAP 6.3,它是 JPA 2.0 但不兼容 2.1,所以现在这是不可能的.

One option is to write a type converter, as defined by JPA 2.1. However, our app server is JBoss EAP 6.3 which is JPA 2.0 but not 2.1 compatible, so this is out of the question for now.

下一个选项是使用 Hibernate 用户类型(有关转换其他 JSR-310 类的博客文章此处).

The next option is to use a Hibernate user type (a blog post about converting other JSR-310 classes here).

有更好的选择吗?谢谢.

Are there better options? Thanks.

推荐答案

在 Hibernate 5.2 之后,这个问题得到了更彻底的解决——你不再需要包含 Ipandzic 的评论中的 hibernate-java8 依赖项和您可以使用 java.time.* 类,例如 LocalDateTimeInstant ,而无需任何额外的步骤.您也不再需要使用 java.util.LocalDateTime 等将列标记为 Temporal,就像使用旧的 java.util.Date 方法.

In Hibernate 5.2 onwards has this issue solved more fully - you no longer need to include the hibernate-java8 dependency from Ipandzic's comment and you can use java.time.* classes such as LocalDateTime or Instant without any additional steps. You also do not need to mark columns using java.util.LocalDateTime etc as Temporal anymore, the way you had to with the older java.util.Date approach.

自 Hibernate 5.2 起,hibernate-java8 内容已合并到 hibernate-core 在这里查看更改说明

Since Hibernate 5.2, the hibernate-java8 content has been merged into hibernate-core see the change notes here

这篇关于使用 JPA2/Hibernate 持久化 java.time.Instant (JDK8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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