【发布时间】:2019-10-04 06:42:33
【问题描述】:
使用以下两个来存储更新和创建时间。
@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(可更新=假) @CreationTimestamp 私有日期创建时间;
serverTimezone=UTC 也已与useLegacyDatetimeCode=false 一起定义
spring.datasource.url=jdbc:mysql://localhost:3306/rmcdb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
【问题讨论】:
标签: mysql hibernate spring-boot