mysql - Hibernate @CreationTimestamp @UpdateTimestamp 在不同的时区
问题描述
使用以下两个来存储更新和创建时间。
@CreationTimestamp
private Timestamp creationTime;
@UpdateTimestamp
private Timestamp updationTime;
当我创建新记录时,这两个记录都存储在UTC
时区中。
+------------+----------------------------------+--------------+--------------+--------------------+--------------------+-----------------------+---------------------------+------+-------------------+---------------------+------------+------------+---------------------+---------------------+----------+
| meeting_id | analysis_description | meeting_date | preread_date | first_version_date | reminder_sent_date | review_addressed_date | review_comments_addressed | tfls | first_record_flag | current_record_flag | created_by | updated_by | creation_time | updation_time | end_time |
+------------+----------------------------------+--------------+--------------+--------------------+--------------------+-----------------------+---------------------------+------+-------------------+---------------------+------------+------------+---------------------+---------------------+----------+
| 4 | BAF312A2122 - CRT 1 - LEGACY CRT | NULL | NULL | NULL | NULL | NULL | NULL | 0 | Y | Y | NULL | NULL | 2019-10-04 06:36:36 | 2019-10-04 06:36:36 | NULL |
+------------+----------------------------------+--------------+--------------+--------------------+--------------------+-----------------------+---------------------------+------+-------------------+---------------------+------------+------------+---------------------+---------------------+----------+
1 row in set (0.00 sec)
但是,如果我更新记录,创建时间戳会更改为我的本地时间戳。
+------------+----------------------------------+--------------+--------------+--------------------+--------------------+-----------------------+---------------------------+------+-------------------+---------------------+------------+------------+---------------------+---------------------+----------+
| meeting_id | analysis_description | meeting_date | preread_date | first_version_date | reminder_sent_date | review_addressed_date | review_comments_addressed | tfls | first_record_flag | current_record_flag | created_by | updated_by | creation_time | updation_time | end_time |
+------------+----------------------------------+--------------+--------------+--------------------+--------------------+-----------------------+---------------------------+------+-------------------+---------------------+------------+------------+---------------------+---------------------+----------+
| 4 | BAF312A2122 - CRT 1 - LEGACY CRT | 2019-09-30 | 2019-09-30 | 2019-09-30 | 2019-09-30 | 2019-09-30 | yes | 1 | Y | Y | NULL | NULL | 2019-10-04 12:08:20 | 2019-10-04 06:38:20 | NULL |
+------------+----------------------------------+--------------+--------------+--------------------+--------------------+-----------------------+---------------------------+------+-------------------+---------------------+------------+------------+---------------------+---------------------+----------+
1 row in set (0.00 sec)
两者都得到更新,而不仅仅是更新时间。
我尝试了各种配置更改,但它们的行为都相同。
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
使用后期构造
@SpringBootApplication public class IdotApplication { @PostConstruct void started() { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); } public static void main(String[] args) { SpringApplication.run(IdotApplication.class, args); }
}
日期而不是时区
@CreationTimestamp 私有日期创建时间;
@CreationTimestamp @Temporal(TemporalType.TIMESTAMP) 私有日期创建时间;
@Column(updatable=false) @CreationTimestamp 私有日期创建时间;
也serverTimezone=UTC
已经与useLegacyDatetimeCode=false
spring.datasource.url=jdbc:mysql://localhost:3306/rmcdb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
解决方案
mysql 5.7+
该问题是由于 mysql 的一个特性引起的,我正在使用liquibase
原始 sql 来生成模式。
如果我在 mysql 中创建一个列,timestamp
它默认自动更新。
mysql> create table test ( a timestamp );
Query OK, 0 rows affected (0.02 sec)
mysql> describe test;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| a | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
1 row in set (0.00 sec)
mysql>
创建新记录时,两个时间戳都在UTC
时区中,因为hibernate
它覆盖了 mysql。
但是,当记录on update current_timestamp
从mysql
火灾中更新时hibernate
,不会做任何事情,因为它@creationTimeStamp
因此保留在mysql
已放置的本地时区中。
为了解决,我将字段定义为
creation_time timestamp null,
updation_time timestamp null
现在mysql
不干扰,更新等都只通过休眠完成。
如果您发现解释有点令人困惑并且正在使用mysql
下面的文章来澄清。
https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
希望它可以帮助某人。
推荐阅读
- ansible - 管理大量 Ansible 角色
- magento2 - bin/magento setup:di:compile throws Source class “-------” for “-------” generation不存在
- azure - Azure 存储队列和消息形状
- flutter - 如何访问 cloud_firestore 中地图字段的值
- javascript - 在 JavaScript 中调用和传递嵌套函数
- c# - System.InvalidOperationException 未处理 HResult=-2146233079 消息=无法创建新服务:ChromeDriverService
- ruby-on-rails - Rails 应用程序中的时髦地图
- c++ - 如何解决与使用预处理器重新定义常用函数名称的标头的命名冲突?
- javascript - 将NodeJS readline保存到变量中?
- java - 将 Arraylist 转换为要通过 Whatsapp 共享的项目列表