在不同的时区休眠@CreationTimestamp @UpdateTimestamp

时间:2019-10-04 06:42:33

标签: mysql hibernate spring-boot

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

@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 私人Date creationTime;

  4. @CreationTimestamp @Temporal(TemporalType.TIMESTAMP) 私人Date creationTime;

  5. @Column(updatable = false) @CreationTimestamp 私人Date creationTime;

serverTimezone=UTCuseLegacyDatetimeCode=false一起已经定义

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

1 个答案:

答案 0 :(得分:0)

该问题是由于mysql的一项功能引起的,我将mysql 5.7+liquibase与原始sql一起用于架构生成。

如果我使用timestamp在mysql中创建一列,它将自动更新。

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,因此它保留在{{1} }已放置。

要解决,我将该字段定义为

mysql

现在creation_time timestamp null, updation_time timestamp null 不会干扰,更新等。所有操作都只能通过休眠来完成。

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

希望它对某人有帮助。