JPA support for Java 8 new date and time APIs

I am using Java 8 for my new project.

I am trying to use the new api date and time in java 8, but I don't know if JPA 2.1 supports this new Date and Time API or not.

Please share your experience / opinion in supporting JPA for the new date and time APIs in Java 8.

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

UPDATE:

I am using Hibernate (4.3.5.Final) as a JPA implementation.

+52
java java-8 jpa java-time
May 18 '14 at 3:50
source share
8 answers

JPA 2.1 is a specification introduced before Java 1.8, so it does not require any support for it. Obviously, some implementations may support some Java 1.8 features. Some of them have problems with Java 1.8 bytecode (e.g. EclipseLink). I know that DataNucleus supports java.time and Java 1.8, since the one I use. You will need to check your implementation, what is its level of support.

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

+18
May 18 '14 at 4:08
source share

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 extra effort. See https://hibernate.atlassian.net/browse/HHH-8844

UPDATE:

Please look at Jeff Morin's comment: since Hibernate 5.2.x 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 Hibernate 5.2 Integration with Spring 4.x framework

+70
Oct 07 '15 at 20:26
source share

I use 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 without problems, but the column data type is a byte in the database data, so you can only read it from a Java application, as far as I know. You can use AttributeConverter to convert new classes to java.sql.Date. I find this code from Java.net

 @Converter(autoApply = true) public class LocalDatePersistenceConverter implements AttributeConverter { @Override public java.sql.Date convertToDatabaseColumn(LocalDate entityValue) { return java.sql.Date.valueOf(entityValue); } @Override public LocalDate convertToEntityAttribute(java.sql.Date databaseValue) { return databaseValue.toLocalDate(); } 
+9
Jul 03 '14 at 9:58
source share

JPA 2.2 supports java.time

JPA 2.2 now supports LocalDate , LocalTime , LocalDateTime , OffsetTime and OffsetDateTime .

 <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> 

You can use Hibernate 5.2 or EclipseLink 2.7 to implement JPA 2.2.

Hibernate 5 supports more java types than JPA 2.2, such as Duration , Instant and ZonedDateTime .

Additional Information:

+9
Sep 04 '17 at 2:31 on
source share

org.jadira.usertype can be used to save the JSR 310 date and time API.

Check out this sample project .

From a sample project

 @MappedSuperclass public class AbstractEntity { @Id @GeneratedValue Long id; @CreatedDate// @Type(type = "org.jadira.usertype.dateandtime.threeten.PersistentZonedDateTime")// ZonedDateTime createdDate; @LastModifiedDate// @Type(type = "org.jadira.usertype.dateandtime.threeten.PersistentZonedDateTime")// ZonedDateTime modifiedDate; } 
+8
Oct 10 '14 at 4:11
source share

I know this is an old question, but I thought of an alternative solution that might be useful.

Instead of matching the new java.time classes. * with existing types of databases, you can use @Transient:

 @Entity public class Person { private Long id; private Timestamp createdTimestamp; @Id @GeneratedValue public Long getId() { return id; } private Timestamp getCreatedTimestamp() { return createdTime; } private void setCreatedTimestamp(final Timestamp ts) { this.createdTimestamp = ts; } @Transient public LocalDateTime getCreatedDateTime() { return createdTime.getLocalDateTime(); } public void setCreatedDateTime(final LocalDateTime dt) { this.createdTime = Timestamp.valueOf(dt); } } 

You work with the public getter / setter methods that use the new Java 8 date and time classes, but behind the scenes getters / setters work with legacy date and time classes. When you save the object, the deprecated date / time property will be saved, but not the new Java 8 property, as it is annotated with @Transient.

+3
Dec 14 '14 at 19:26
source share

For type TIMESTAMP you can use this converter:

 @Converter(autoApply = true) public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> { @Override public Timestamp convertToDatabaseColumn(LocalDateTime datetime) { return datetime == null ? null : Timestamp.valueOf(datetime); } @Override public LocalDateTime convertToEntityAttribute(Timestamp timestamp) { return timestamp == null ? null : timestamp.toLocalDateTime(); } } 

For type DATE, you can use this converter:

 @Converter(autoApply = true) public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> { @Override public Date convertToDatabaseColumn(LocalDate date) { return date == null ? null : Date.valueOf(date); } @Override public LocalDate convertToEntityAttribute(Date date) { return date == null ? null : date.toLocalDate(); } } 

For type TIME you can use this converter:

 @Converter(autoApply = true) public class LocalTimeAttributeConverter implements AttributeConverter<LocalTime, Time> { @Override public Time convertToDatabaseColumn(LocalTime time) { return time == null ? null : Time.valueOf(time); } @Override public LocalTime convertToEntityAttribute(Time time) { return time == null ? null : time.toLocalTime(); } } 
+1
Oct 10 '17 at 16:59 on
source share

There are many ways to do it, and it depends on your frame work: If your framework work has such a spring on the Converter field, follow these steps: 1 -

 @DateTimeFormat(pattern = "dd.MM.yyyy - HH:mm") private Long createdDate; 

here I use the format of the obsolete era https://www.epochconverter.com/ era is very flexible and accepted format

2- Other ways is to use jpa @PostLoad @PreUpdate @PrePersist

 @PostLoad public void convert() { this.jva8Date= LocalDate.now().plusDays(1); } 

or use the pace one such

 @Transient public LocalDateTime getCreatedDateTime() { return createdTime.getLocalDateTime(); } 
0
Aug 27 '17 at 10:15
source share



All Articles