Spring Boot JPA Hibernate-以毫秒精度存储日期



Spring Boot版本'2.3.4.REASE'
Java 11
org.springframework.Boot:Spring Boot starter jdbc
org/springframework.Boot:Spring Boot-starter data jpa
Spring-data-jpa-2.3.4.RELEASE
运行时(mysql:mysql-connector Java(

服务器数据库MariaDB(10.5.5-MariaDB版本(
Java MariaDB连接器J:2.6.0[稳定]

我试图在Hibernate中以毫秒精度持久化java.sql.Timestamp对象。我需要用毫秒将日期保存到数据库。例如:2020-10-08 03:23:38.454。

我的域名:

import java.sql.Timestamp;
@Entity
@Data
@Table(name = "date_test")
public class DateTestDomain {
@Id
@Column(nullable = false, name = "date", columnDefinition = "TIMESTAMP(3)")
@Temporal(TIMESTAMP)
private Calendar dateTest;
}

我的回购:

@Repository
public interface DateTestRepo extends JpaRepository<DateTestDomain, Timestamp> {
}

将日期保存到数据库:

private final JdbcTemplate db;
...
long testTime = 1602120218454L;
Timestamp dateTimeStamp = new Timestamp(testTime);
db.update("INSERT INTO date_test" + " (date) VALUES( "" + dateTimeStamp + "")");

UPD:sql的结果正是我所需要的!!!这种方法非常有效:2020-10-08 03:23:38.454

但是使用hibernate/JPA的结果是FALSE
调试跟踪:
2020年10月9日22:26:53.120休眠:插入日期测试(日期(值(?(
2010年10月19日22:26:53.122 Trace 95038-[restartedMain]o.h.type.descriptor.sql.BasicBinder:绑定参数[1]为[TIMESTAMP]-[2020年10日9日22:26:53.44]

Calendar calendar = Calendar.getInstance();
Date date = new Date();
calendar.setTimeInMillis(date.getTime());
dateTestDomain.setDateTest(calendar);
dateTestRepo.save(dateTestDomain);

sql的结果:分数秒总是设置为.000,并使用hibernate sql插入:

2020-10-09 22:26:53.000

请提供帮助。我需要用毫秒精度的投掷JPA来节省到数据库的时间。

UPD:

我尝试sql方言:
org.hibernate.dialect.MySQL5InnoDB方言比org.hibernated.dialect.mySQL55InnoDB方言比org.hibernat.dialect.MariaDB103方言比org.hibernate.t方言.MariaDB105方言
,但没有成功
UPD 1:
休眠:插入日期测试(时间戳,本地日期时间,本地日期日期时间(值(NOW(3(,?,?(
2020年10月10日15:33:29.999 TRACE 44072---[restartedMain]o.h.type.descriptor.sql.BasicBinder:绑定参数[1]为[TIMESTAMP]-[2020年10日15:32:29.051]
2020月10日15时33:29.100 TRACE 44072-[restarted Main]o.h.type-descriptor.sql.BasicBinder:绑定参数[2]为[TIMESTAMP]-[java.util.GregorianLendar[time=1602336809051,areFieldsSet=true…

结果SQL:
20200-10 15:33:29.10120200-10 13:33:29.0002020-10 15:33:29.000。

还有一个问题:
DB日期:
2020年10月10日16:19:42.578
2022年10月16日20:47.000
2020-10年10月16:20:47.888
2021-10年10日16:20:47.892
2002020年10日16时20:47.896
2027-10年16时20分47.900
休眠:从date_test datetestdo0_中选择datetestdo0.timestamp作为时间戳1_0_,其中datetestdoo_timestamp>
绑定参数[1]为[TIMESTAMP]-[20200-10 16:20:47.893]
2020-10-10 16:20:47.888
2020-10-10 17:20:47.892
2020 0-10 16:20-47.896
1020 0-10 18:20:47.9

jdbcsql:
从date_test中选择时间戳,其中时间戳>quot;2020-10-10 16:20:47.893">
2020-10-10 16:20:47.896
2020-10-10 16:20:47.900

jpa/hibernate无法使用毫秒。。。

由于JPA 2.2,因此支持java8日期和时间API。我还没有尝试过它是否能解决你的问题,但你能尝试使用java8的LocalDateTime而不是Calendar类型吗。

替换:

@Id
@Column(nullable = false, name = "date", columnDefinition = "TIMESTAMP")
@Temporal(TIMESTAMP)
private LocalDateTime localDateTime;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(calendar.getTimeInMillis());

试试这个,而不是使用新的日期((

这不是一个直接的解决方案,而是一个变通方案,效果非常好:不存储实际日期,只需存储日期的long值,通过调用date.getTime()方法,存储此结果,当从日期中获取日期时,使用new Date(longValue)使用存储在数据库中的longValue重新创建原始日期,这种方法至少保留了毫秒精度的

最新更新