带有 JPA 的 WildFly 时区 [英] WildFly Timezone with JPA

查看:48
本文介绍了带有 JPA 的 WildFly 时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Hibernate 和 WildFly 开发一个项目.我使用的数据库是 mariaDB.

I currently develop a project with Hibernate and WildFly. The database I'm using is a mariaDB.

当我将 java.time.Instant 插入数据库时​​,JPA 在插入的时间戳(在 java 代码中始终是 UTC)上应用两个小时.

When I insert a java.time.Instant into the database, the JPA applies two hours on the inserted timestamp (which is always UTC in the java code).

我尝试在 standalone.conf (Java Opts) 和命令行参数中使用 -Duser.timezone=UTC 启动 WildFly.管理控制台显示此时区,但所有日志输出和数据库输入都在我的系统时区 (+2) 中.当我将 Windows 时区设置为 UTC 时,奇怪的行为停止并且数据库中的时间是正确的.

I tried to start WildFly with -Duser.timezone=UTC in the standalone.conf (Java Opts) and command-line argument. The management console shows this timezone, but all log-output and database inputs are in my system timezone (+2). When I set my Windows timezone to UTC, the odd behaviour stops and the times in the database are correct.

我还尝试设置休眠属性 <property name="hibernate.jdbc.time_zone" value="UTC"/> 和我的 mariaDB SET GLOBAL time_zone = "+00:00" 效果不佳.

I also tried to set the hibernate property <property name="hibernate.jdbc.time_zone" value="UTC"/> and on my mariaDB SET GLOBAL time_zone = "+00:00" which didn't work as well.

我在这里错过了什么?

推荐答案

所以这个问题的答案很简单.

So the answer to this problem was rather easy.

我使用了带有 WildFly12 的 Hibernate 版本 - 即 5.1.10.根据 克里斯克兰福德

I used the Hibernate version packed with WildFly12 - which is 5.1.10. According to Chris Cranford

直到 5.2.3 才引入 hibernate.jdbc.time_zone 设置

the hibernate.jdbc.time_zone setting wasn't introduced until 5.2.3

尽管这里的任何子系统都没有抛出错误.更新您的 Hibernate 即可解决问题.

although no error was thrown by any subsystem here. Updating your Hibernate will do the trick.

  1. 进入wildfly-12.0.0.Final\modules\system\layers\base\org\hibernate\main
  2. 下载核心envers 库并将其放入文件夹中.
  3. 修改 module.xml 并更新版本并删除 -java8-* 和 entitymanger-*.jar -> 它们已弃用并移入核心.
  4. 重新启动您的 WildFly,您的问题就会消失.
  1. Go into wildfly-12.0.0.Final\modules\system\layers\base\org\hibernate\main
  2. Download the core and envers library and put it into the folder.
  3. Modify the module.xml and update the versions and remove the -java8-* and entitymanger-*.jar -> they are depreacted and moved into the core.
  4. Restart your WildFly and your problems will be gone.

WildFly 团队没有使用更新的 Hibernate 版本可能有一个很好的理由,但我不确定,为什么.因此,交换的库可能存在一些错误/不需要的行为.

There might be a good reason why the WildFly team didn't use a newer Hibernate version, but I'm not sure, why. So it could be that you have some errors / unwanted behaviour with the swapped libraries.

实际上,属性未知的简单错误消息本可以为我节省一些时间.要么我没有在日志中找到它,要么它不是错误...

Actually, a simple error message that the property is unknown could have saved me some time. Either I didn't find it within the logs or it's just not a error...

这篇关于带有 JPA 的 WildFly 时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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