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.
I know that this is an old question but I thought of an alternative solution which might be helpful.
Instead of trying to map the new java.time.* classes to an existing database types, you can leverage @Transient:
You work with the public getter/setter methods that use the new Java 8 date/time classes, but behind the scenes the getter/setters work with the legacy date/time classes. When you persist the entity, the legacy date/time property will be persisted but not the new Java 8 property since it's annotated with @Transient.
org.jadira.usertype can be used to persist JSR 310 Date and Time API.
Check out this example project.
From Example Project,
JPA 2.2 supports java.time
JPA 2.2 now supports
LocalDate
,LocalTime
,LocalDateTime
,OffsetTime
andOffsetDateTime
.For JPA 2.2 implementation, Hibernate 5.2 or EclipseLink 2.7 can be used.
Hibernate 5 supports more java types than JPA 2.2 like
Duration
,Instant
andZonedDateTime
.More Info:
I am using Java 8, EclipseLink(JPA 2.1),PostgreSQL 9.3 and PostgreSQL Driver -Postgresql-9.2-1002.jdbc4.jar in my project and i can use LocalDateTime variables from the new API with no problem but the data type of the column is bytea in the database, so you only can read it from a Java aplication as far i know. You can use AttributeConverter to convert the new classes to java.sql.Date I find this code from Java.net
There are Many approach to do , Also It depends to your frame work : If your frame work has on field Converter such spring do such: 1-
here I am using legacy epoch format https://www.epochconverter.com/ epoch is very flexible and accepted format
2- The other ways is to use jpa @PostLoad @PreUpdate @PrePersist
or use temp one such
For type TIMESTAMP you can use this converter:
For type DATE you can use this converter:
For type TIME you can use this converter: