Reputation: 1
In my spring boot framework with spring-data-jpa,
when I INSERT or UPDATE with javax.persistence.EntityManager, MySQL database is inserted without milli seconds.
in my spring boot framework, I'm working with
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);
I tested with Table's columns with DATETIME(3) and DATETIME(6)
MySQL Table :
I tried using java.util.Date, java.util.Calendar and java.time.LocalDateTime.
I also tried with @CreationTimestamp annotation on my parameter of entity class and also, manually set each parameter's values.
and every time, I get my TRACE level log with milliseconds included.
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');
But in my database as a result, all the milli seconds are removed
Result Table:
Upvotes: 0
Views: 798
Reputation: 1
I still don't know why my original code removes milliseconds but I found other way to solve the problem.
for column such as create_date which needs to be set when first INSERT, use
'create_date' DATETIME(3) NOT NULL DEFAULT current_timestamp(3)
and
@Column(
insertable = false
,updatable = false
)
private LocalDateTime createDate;
for column like update_date which needs to be updated each time UPDATE executed,
'update_date' DATETIME(6) NOT NULL DEFAULT current_timestamp(3) ON UPDATE current_timestamp(3)
and entity to
@Column(updatable = false)
private LocalDateTime updateDate;
In this way, each DATETIME columns will be set by database's server time which is better for my situation.
Upvotes: 0