Spring版本2.3.4 version
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-连接器-java)
服务器DB MariaDB (ver.10.5.5-MariaDB)
Java MariaDB连接器J:2.6.0稳定
我试图以毫秒的精度在Hibernate中持久化一个java.sql.Timestamp对象。我需要用毫秒将日期保存到db。例如: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> {
}
将日期保存到db:
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 :23:38.454
但是对于hibernate/JPA,结果是假的。
调试跟踪:
2020年-10-09 22:26:53.120 Hibernate:插入date_test (日期)值(?)
2020年-10-09 22:26:53.122 TRACE 95038 -- restartedMain o.h.type.descriptor.sql.BasicBinder :绑定参数1作为时间戳- 2020-10-09 22:26:53.044
Calendar calendar = Calendar.getInstance();
Date date = new Date();
calendar.setTimeInMillis(date.getTime());
dateTestDomain.setDateTest(calendar);
dateTestRepo.save(dateTestDomain);
sql的结果:使用hibernate sql,小数秒总是设置为.000:
2020-10-09 22:26:53.000
请帮帮忙。我需要节省到分贝时间与毫秒精度抛JPA。
UPD:
我尝试使用sql方言:
org.hibernate.dialect.MySQL5InnoDBDialect比org.hibernate.dialect.MySQL55InnoDBDialect比org.hibernate.dialect.MariaDB103Dialect比org.hibernate.dialect.MariaDB105Dialect
但没有成功。
UPD 1:
Hibernate:插入到date_test (时间戳,local_date_time,local_date_timea)值(现在(3),?
2020年-10-10 15:33:29.099跟踪44072 -- restartedMain o.h.type.descriptor.sql.BasicBinder :绑定参数1作为时间戳- 2020-10-10 15:33:29.051
2020年-10-10 15:33:29.100跟踪44072 - [java.util.GregorianCalendar[time=1602336809051,o.h.type.descriptor.sql.BasicBinder :绑定参数2作为时间戳- restartedMain areFieldsSet=true.
结果SQL:
2020年-10-10 15:33:29.101,2020年-10-10 13:29.000,2020年-10-10 15:33:29.000.
还有一个问题:
DB日期:
2020年-10-10 16:19:42.578
2020年-10-10 16:20:47.000
2020年-10-10 16:20:47.888
2020年-10-10 16:20:47.892
2020年-10-10 16:20:47.896
2020年-10-10 16:20:47.900
Hibernate:选择datetestdo0_.timestamp作为timestam1__,从date_test datetestdo0_中选择datetestdo0_.timestamp>?
绑定参数1作为时间戳- 2020-10-10 16:20:47.893
2020年-10-10 16:20:47.888
2020年-10-10 16:20:47.892
2020年-10-10 16:20:47.896
2020年-10-10 16:20:47.9
jdbcsql:
从date_test timestamp>中选择时间戳“2020-10-10 16:20:47.893”
2020年-10-10 16:20:47.896
2020年-10-10 16:20:47.900
jpa/hibernate不工作毫秒..。
发布于 2020-10-10 08:55:39
自从JPA2.2以来,就有了对java8日期和时间API的支持。我没有尝试过它是否能解决您的问题,但是您是否可以尝试使用java8 8的LocalDateTime
而不是Calendar
类型。
取代:
@Id
@Column(nullable = false, name = "date", columnDefinition = "TIMESTAMP")
@Temporal(TIMESTAMP)
private LocalDateTime localDateTime;
发布于 2020-10-10 10:43:38
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(calendar.getTimeInMillis());
试试这个,而不是使用新的日期()
发布于 2022-10-03 20:45:46
这不是一个直接的解决方案,而是一种完美的工作:与其存储实际日期,不如存储日期的long
值,通过调用date.getTime()
方法来存储此结果,当从日期获取日期时使用数据库中存储的longValue
重新创建原始日期时,这种方法至少可以保持毫秒的精度。
https://stackoverflow.com/questions/64262300
复制相似问题