Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @Temporal for Java 8 java.time.Instant

Tags:

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?

like image 780
Dmitry Adonin Avatar asked Feb 19 '16 14:02

Dmitry Adonin


People also ask

Does hibernate support instant?

Hibernate supports Instant just fine already and allows changing the database timezone via hibernate. jdbc.

Does JPA support instant?

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.

What is @temporal TemporalType TIMESTAMP?

@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.

What is @temporal in hibernate?

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.


1 Answers

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/

like image 120
HAYMbl4 Avatar answered Oct 05 '22 11:10

HAYMbl4