gpt4 book ai didi

mysql - 不同时区的 Hibernate @CreationTimestamp @UpdateTimestamp

转载 作者:行者123 更新时间:2023-11-29 15:35:57 25 4
gpt4 key购买 nike

使用以下两个来存储更新和创建时间。

@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)

而且两者都会更新,而不仅仅是更新时间。

我尝试了各种配置更改,但它们的行为都是相同的。

  1. spring.jpa.properties.hibernate.jdbc.time_zone=UTC

  2. 使用后置构造

    @SpringBootApplication
    public class IdotApplication {

    @PostConstruct
    void started() {
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    }

    public static void main(String[] args) {
    SpringApplication.run(IdotApplication.class, args);
    }

    }

  3. 日期而不是时区

    @CreationTimestamp私有(private)日期创建时间;

  4. @CreationTimestamp@Temporal(TemporalType.TIMESTAMP)私有(private)日期创建时间;

  5. @Column(可更新=假)@CreationTimestamp私有(private)日期创建时间;

此外,serverTimezone=UTC 已与 useLegacyDatetimeCode=false 一起定义

spring.datasource.url=jdbc:mysql://localhost:3306/rmcdb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

最佳答案

这个问题是由于mysql的一个功能引起的,我使用mysql 5.7+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 from mysql 会触发,而 hibernate 不会执行任何操作,因为它是 @creationTimeStamp 因此它保留在 mysql 所设置的本地时区中。

为了解决这个问题,我将该字段定义为

creation_time timestamp null,
updation_time timestamp null

现在mysql不会干扰、更新等。所有这些都仅通过 hibernate 完成。

如果您发现解释有点令人困惑并且正在使用mysql,请阅读下面的文章以进行澄清。 https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

希望它对某人有帮助。

关于mysql - 不同时区的 Hibernate @CreationTimestamp @UpdateTimestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58231167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com