Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA support for Java 8 new date and time API

I'm using Java 8 for my new project.

I'm trying to use new date and time api in java 8 however I don't know if JPA 2.1 fully supports this new Date and Time API or not.

Please share your experience/opinion in JPA`s supports for new date and time API in Java 8.

Can I use new date and time api in Java 8 safely with JPA 2.1?

UPDATE:

I'm using Hibernate (4.3.5.Final) as JPA implementation.

like image 443
TheKojuEffect Avatar asked May 18 '14 03:05

TheKojuEffect


2 Answers

For Hibernate 5.X just add

    <dependency>         <groupId>org.hibernate</groupId>         <artifactId>hibernate-java8</artifactId>         <version>${hibernate.version}</version>     </dependency> 

and

@NotNull @Column(name = "date_time", nullable = false) protected LocalDateTime dateTime; 

will work without any additional effort. See https://hibernate.atlassian.net/browse/HHH-8844

UPDATE:

Please have a look at Jeff Morin comment: since Hibernate 5.2.x it is enough

 <dependency>      <groupId>org.hibernate</groupId>      <artifactId>hibernate-core</artifactId>      <version>5.2.1.Final</version>  </dependency>  <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-...</artifactId>      <version>4.3.1.RELEASE</version>  </dependency> 

See https://github.com/hibernate/hibernate-orm/wiki/Migration-Guide---5.2 and Integrate Hibernate 5.2 with Spring framework 4.x

like image 70
Grigory Kislin Avatar answered Sep 23 '22 14:09

Grigory Kislin


JPA 2.1 is a spec that came out before Java 1.8, so doesn't mandate any support for it. Obviously some implementations may support some Java 1.8 features. Some have problems with Java 1.8 bytecode (e.g EclipseLink). I know DataNucleus supports java.time and Java 1.8 since that's the one I use. You'd have to check your implementation for what its support level is.

It has been requested that JPA 2.2 support the java.time types, see this issue https://java.net/jira/browse/JPA_SPEC-63

like image 21
Neil Stockton Avatar answered Sep 21 '22 14:09

Neil Stockton