休眠保存/检索日期减去一天,如果应用程序使用MySQL以外的其他时区
我有一个应用程序在MACHINE_A的tomcat上启动,时区为GMT + 3。
我使用MACHINE_B与时区UTC启动的远程MySQL服务器。
我们使用 spring-data-jpa 来实现持久性。
作为问题的一个例子,我将展示存储库:
public interface MyRepository extends JpaRepository<MyInstance, Long> {
Optional<MyInstance> findByDate(LocalDate localDate);
}
如果我传递 localDate for ,我会得到日期为(前一天)的实体2018-09-06
2018-09-05
在我看到的日志中:
2018-09-06 18:17:27.783 TRACE 13676 --- [nio-8080-exec-3] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [DATE] - [2018-09-06]
我在谷歌上搜索了这个问题,发现了几篇内容相同的文章(例如 https://moelholm.com/2016/11/09/spring-boot-controlling-timezones-with-hibernate/)
所以,我有以下几点:application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/MYDB?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: *****
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
properties:
hibernate:
show_sql: true
use_sql_comments: true
format_sql: true
type: trace
jdbc:
time_zone: UTC
但这无济于事。
我们使用以下连接器:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
如何解决我的问题?
附言
我试图用相同的时区运行这两个应用程序。在这种情况下,一切按预期工作。
附言 2
我尝试使用MySQL驱动程序6.0.6版本,但它没有改变任何东西。