Spring-JPA在插入或更新时删除毫秒

1l5u6lss  于 2023-05-18  发布在  Spring
关注(0)|答案(1)|浏览(235)

在我的带有spring-data-jpa的spring Boot 框架中
当我使用javax.persistence.EntityManager进行INSERT或UPDATE时,MySQL数据库插入不需要毫秒
在我的Sping Boot 框架中,我使用
spring-data-jpa 2.7.11
mysql-connector-j 8.0.33
hibernate-core 5.6.15.Final

javax.persistence.EntityManager entityManager;
entityManager.persist(myEntityItem);

我测试了Table的列DATETIME(3)和DATETIME(6)

MySQL表:

我尝试使用java.util.Date、java.util.Calendar和java. time. LocalDateTime。
我还尝试在实体类的参数上使用@CreationTimestamp注解,并手动设置每个参数的值。
每一次,我都会得到包含毫秒的TRACE级别日志。

Hibernate: 
    insert 
    into
        DREAM_TEST.time_table
        (calendar3, calendar6, java_util_date3, java_util_date6, local_date_time3, local_date_time6, text) 
    values
        (?, ?, ?, ?, ?, ?, ?)
2023-05-17 10:02:27.685 TRACE 31760 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [java.util.GregorianCalendar[time=1684285347672,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Seoul",offset=32400000,dstSavings=0,useDaylight=false,transitions=30,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2023,MONTH=4,WEEK_OF_YEAR=20,WEEK_OF_MONTH=3,DAY_OF_MONTH=17,DAY_OF_YEAR=137,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=2,SECOND=27,MILLISECOND=672,ZONE_OFFSET=32400000,DST_OFFSET=0]]
2023-05-17 10:02:27.685 TRACE 31760 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [TIMESTAMP] - [java.util.GregorianCalendar[time=1684285347672,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Seoul",offset=32400000,dstSavings=0,useDaylight=false,transitions=30,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2023,MONTH=4,WEEK_OF_YEAR=20,WEEK_OF_MONTH=3,DAY_OF_MONTH=17,DAY_OF_YEAR=137,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=2,SECOND=27,MILLISECOND=672,ZONE_OFFSET=32400000,DST_OFFSET=0]]
2023-05-17 10:02:27.685 TRACE 31760 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [TIMESTAMP] - [2023-05-17 10:02:27.6725819]
2023-05-17 10:02:27.685 TRACE 31760 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [TIMESTAMP] - [2023-05-17 10:02:27.6725819]
2023-05-17 10:02:27.685 TRACE 31760 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [TIMESTAMP] - [2023-05-17T10:02:27.672581900]
2023-05-17 10:02:27.686 TRACE 31760 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [TIMESTAMP] - [2023-05-17T10:02:27.672581900]
2023-05-17 10:02:27.686 TRACE 31760 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [7] as [VARCHAR] - [with values set]
2023-05-17 10:02:27.691  INFO 31760 --- [           main] p6spy                                    : #1684285347691 | took 2ms | statement | connection 9| url jdbc:mysql://dreamtest.db:4306/DREAM_TEST
insert into DREAM_TEST.time_table (calendar3, calendar6, java_util_date3, java_util_date6, local_date_time3, local_date_time6, text) values (?, ?, ?, ?, ?, ?, ?)
insert into DREAM_TEST.time_table (calendar3, calendar6, java_util_date3, java_util_date6, local_date_time3, local_date_time6, text) values ('2023-05-17T10:02:27.672+0900', '2023-05-17T10:02:27.672+0900', '2023-05-17T10:02:27.672+0900', '2023-05-17T10:02:27.672+0900', '2023-05-17T10:02:27.672+0900', '2023-05-17T10:02:27.672+0900', 'with values set');

但在我的数据库中,所有的毫秒都被删除了

结果表:

h7wcgrx3

h7wcgrx31#

我仍然不知道为什么我的原始代码删除毫秒,但我找到了其他方法来解决这个问题。
对于第一次INSERT时需要设置的列,如create_date,使用

'create_date' DATETIME(3) NOT NULL DEFAULT current_timestamp(3)

@Column(
        insertable = false
        ,updatable = false
)
private LocalDateTime createDate;

对于像update_date这样每次执行UPDATE都需要更新的列,

'update_date' DATETIME(6) NOT NULL DEFAULT current_timestamp(3) ON UPDATE current_timestamp(3)

和实体

@Column(updatable = false)
private LocalDateTime updateDate;

这样,每个DATETIME列将由数据库的服务器时间设置,这对我的情况更好。

相关问题