Reputation: 616
I've switched to Java 8 and Hibernate 5 to overcome the problem of not being able to store milliseconds in Hibernate.
private LocalDateTime date = LocalDateTime.now();
public LocalDateTime getDate() {
return date;
}
Maven dependencies
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.0.1.Final</version>
</dependency>
Despite this Hibernate is still storing the LocalDateTime object as tinyblob. What am I missing? I'm using MySQL 5.6.19
According to this
Hibernate Issue, the hibernate-java8 module should map this to TIMESTAMP. I've tried placing a @Column(columnDefinition="TIMESTAMP)
on the getter, but this led to a DataIntegrityViolationException.
Placed @Type(type="org.hibernate.type.LocalDateTimeType")
on the getter. Database table still persisting as tinyblob.
Upvotes: 4
Views: 2176
Reputation: 2416
As of this comment:
Hibernate provides type converters for all the JDK date/time types:
OffsetDateTime
getters on entities should be annotated with @Type(type= "org.hibernate.type.OffsetDateTimeType")
LocalDateTime
getters on entities should be annotated with @Type(type= "org.hibernate.type.LocalDateTimeType")
...etc.
Upvotes: 3
Reputation: 63
The reason it stores it as a TINYBLOB is that JPA 2.1 was released before Java 8 and the Date and Time API didn’t exist at that point in time.
To overcome this, you will need to define the mapping to java.sql.Date or java.sql.Timestamp yourself.
Upvotes: 0
Reputation: 1216
Mysql's timestamp does not have millisecond precision so it wouldn't be able to store it as a timestamp, and probably detects this and saves it as a blob.
Upvotes: 0