I use Hibernate and try to do the following:
@Temporal(TemporalType.DATE) @Column(name = "BIRTHDAY") private Instant birthday;
but have this exception:
Caused by: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property:
As was suggested I tried to use
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-java8</artifactId> <version>${hibernate.ver}</version> </dependency>
but have the same exception. Should I do some additional steps or how to resolve such a case?
Hibernate supports Instant just fine already and allows changing the database timezone via hibernate. jdbc.
time. Instant is the best choice to store a date into DB: it is the most likely TIMESTAMP and you are not depending by timezone, it is just a moment on the time. JPA supports LocalDate , LocalTime , LocalDateTime etc. but not Instant.
@Temporal is a JPA annotation that converts back and forth between timestamp and java. util. Date . It also converts time-stamp into time. For example, in the snippet below, @Temporal(TemporalType.
The @Temporal annotation has the single parameter value of type TemporalType. It can be either DATE, TIME or TIMESTAMP, depending on the underlying SQL type that we want to use for the mapping.
You can not use @Temporal
with java 8 time, if you want date with time use LocalDateTime
, if you want only date use LocalDate
without @Temporal
anotation.
look also http://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With