Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Time API with Hibernate using millisecond / nanosecond precision

I have a very simple entity, that uses the new Java 8 Time API.

@Entity
@Table(name = "event")
public class Event {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private long id;

    @Column(name = "time")
    private LocalDateTime time;

    // ommitted
}

To properly support conversion, im using

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.1.0.Final</version>
</dependency>

However, records in the database are saved like 2018-05-12 22:24:31, which completely forgets the nanoseconds from LocalTime. I know that neither Timestamp nor Date can store nanoseconds, but i need at least millisecond precision. Is there any possibility to achieve this?

If relevant, I am using a MySQL database.

like image 837
Glains Avatar asked Oct 15 '25 13:10

Glains


1 Answers

Change column type to

DATETIME(6) 

as curently your second fraction is lost due to integer nature of DATETIME Here you have MySQL documentation for that topic https://dev.mysql.com/doc/refman/8.0/en/fractional-seconds.html

Quoting

DATETIME(fsp)

The fsp value, if given, must be in the range 0 to 6. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0. (This differs from the standard SQL default of 6, for compatibility with previous MySQL versions.)

like image 128
Antoniossss Avatar answered Oct 18 '25 03:10

Antoniossss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!