Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Date in Hibernate with millisecond precision

I'm trying to persist a Date object in Hibernate with millisecond precision.

private Date date = new Date();

@Column(columnDefinition="DATETIME(3)")
public Date getDate() {
    return date;
}

But in the database the millisecond component is always .000. Is it simply not possible to store a Date object with millisecond precision using Hibernate?

like image 522
AlyoshaKaramazov Avatar asked Jan 23 '26 02:01

AlyoshaKaramazov


1 Answers

java.util.date will only persist down to the second, since this is mapped to an SQL DATE type by the ORM, which may not contain milliseconds (at least in implementations I'm familiar with).

java.sql.timestamp supports fractional seconds.

I would definitely look into using the new Java 8 date libraries if you're able (or joda if you can't use Java 8). They're much better.

like image 159
StuPointerException Avatar answered Jan 24 '26 16:01

StuPointerException