jpa 2.2 support for java 8 date and time types.

This commit is contained in:
eelhazati 2019-01-10 16:22:07 +01:00
parent 3bad1ab7d3
commit 2432527980
5 changed files with 310 additions and 5 deletions

View File

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>java-jpa</artifactId>
<name>java-jpa</name>
<name>java-jpa</name>
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
@ -24,10 +24,35 @@
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<!--Compile time JPA API-->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<!--Runtime JPA implementation-->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.version}</version>
<scope>runtime</scope>
<type>bundle</type>
</dependency>
</dependencies>
<properties>
<hibernate.version>5.3.1.Final</hibernate.version>
<hibernate.version>5.4.0.Final</hibernate.version>
<h2.version>1.4.197</h2.version>
<eclipselink.version>2.7.4-RC1</eclipselink.version>
<postgres.version>42.2.5</postgres.version>
</properties>
</project>

View File

@ -0,0 +1,68 @@
package com.baeldung.jpa.datetime;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.*;
import java.util.Calendar;
public class DateTimeEntityRepository {
private EntityManagerFactory emf = null;
public DateTimeEntityRepository() {
emf = Persistence.createEntityManagerFactory("java8-datetime-postgresql");
}
public JPA22DateTimeEntity find(Long id) {
EntityManager entityManager = emf.createEntityManager();
JPA22DateTimeEntity dateTimeTypes = entityManager.find(JPA22DateTimeEntity.class, id);
entityManager.close();
return dateTimeTypes;
}
public void save(Long id) {
JPA22DateTimeEntity dateTimeTypes = new JPA22DateTimeEntity();
dateTimeTypes.setId(id);
//java.sql types: date/time
dateTimeTypes.setSqlTime(Time.valueOf(LocalTime.now()));
dateTimeTypes.setSqlDate(Date.valueOf(LocalDate.now()));
dateTimeTypes.setSqlTimestamp(Timestamp.valueOf(LocalDateTime.now()));
//java.util types: date/calendar
java.util.Date date = new java.util.Date();
dateTimeTypes.setUtilTime(date);
dateTimeTypes.setUtilDate(date);
dateTimeTypes.setUtilTimestamp(date);
//Calendar
Calendar calendar = Calendar.getInstance();
dateTimeTypes.setCalendarTime(calendar);
dateTimeTypes.setCalendarDate(calendar);
dateTimeTypes.setCalendarTimestamp(calendar);
//java.time types
dateTimeTypes.setLocalTime(LocalTime.now());
dateTimeTypes.setLocalDate(LocalDate.now());
dateTimeTypes.setLocalDateTime(LocalDateTime.now());
//java.time types with offset
dateTimeTypes.setOffsetTime(OffsetTime.now());
dateTimeTypes.setOffsetDateTime(OffsetDateTime.now());
EntityManager entityManager = emf.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(dateTimeTypes);
entityManager.getTransaction().commit();
entityManager.close();
}
public void clean() {
emf.close();
}
}

View File

@ -0,0 +1,176 @@
package com.baeldung.jpa.datetime;
import javax.persistence.*;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.*;
import java.util.Calendar;
@Entity
public class JPA22DateTimeEntity {
@Id
private Long id;
//java.sql types
private Time sqlTime;
private Date sqlDate;
private Timestamp sqlTimestamp;
//java.util types
@Temporal(TemporalType.TIME)
private java.util.Date utilTime;
@Temporal(TemporalType.DATE)
private java.util.Date utilDate;
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date utilTimestamp;
//Calendar
@Temporal(TemporalType.TIME)
private Calendar calendarTime;
@Temporal(TemporalType.DATE)
private Calendar calendarDate;
@Temporal(TemporalType.TIMESTAMP)
private Calendar calendarTimestamp;
// java.time types
@Column(name = "local_time", columnDefinition = "TIME")
private LocalTime localTime;
@Column(name = "local_date", columnDefinition = "DATE")
private LocalDate localDate;
@Column(name = "local_date_time", columnDefinition = "TIMESTAMP")
private LocalDateTime localDateTime;
@Column(name = "offset_time", columnDefinition = "TIME WITH TIME ZONE")
private OffsetTime offsetTime;
@Column(name = "offset_date_time", columnDefinition = "TIMESTAMP WITH TIME ZONE")
private OffsetDateTime offsetDateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Time getSqlTime() {
return sqlTime;
}
public void setSqlTime(Time sqlTime) {
this.sqlTime = sqlTime;
}
public Date getSqlDate() {
return sqlDate;
}
public void setSqlDate(Date sqlDate) {
this.sqlDate = sqlDate;
}
public Timestamp getSqlTimestamp() {
return sqlTimestamp;
}
public void setSqlTimestamp(Timestamp sqlTimestamp) {
this.sqlTimestamp = sqlTimestamp;
}
public java.util.Date getUtilTime() {
return utilTime;
}
public void setUtilTime(java.util.Date utilTime) {
this.utilTime = utilTime;
}
public java.util.Date getUtilDate() {
return utilDate;
}
public void setUtilDate(java.util.Date utilDate) {
this.utilDate = utilDate;
}
public java.util.Date getUtilTimestamp() {
return utilTimestamp;
}
public void setUtilTimestamp(java.util.Date utilTimestamp) {
this.utilTimestamp = utilTimestamp;
}
public Calendar getCalendarTime() {
return calendarTime;
}
public void setCalendarTime(Calendar calendarTime) {
this.calendarTime = calendarTime;
}
public Calendar getCalendarDate() {
return calendarDate;
}
public void setCalendarDate(Calendar calendarDate) {
this.calendarDate = calendarDate;
}
public Calendar getCalendarTimestamp() {
return calendarTimestamp;
}
public void setCalendarTimestamp(Calendar calendarTimestamp) {
this.calendarTimestamp = calendarTimestamp;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public OffsetTime getOffsetTime() {
return offsetTime;
}
public void setOffsetTime(OffsetTime offsetTime) {
this.offsetTime = offsetTime;
}
public OffsetDateTime getOffsetDateTime() {
return offsetDateTime;
}
public void setOffsetDateTime(OffsetDateTime offsetDateTime) {
this.offsetDateTime = offsetDateTime;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.jpa.datetime;
public class MainApp {
public static void main(String... args) {
DateTimeEntityRepository dateTimeEntityRepository = new DateTimeEntityRepository();
//Persist
dateTimeEntityRepository.save(100L);
//Find
JPA22DateTimeEntity dateTimeEntity = dateTimeEntityRepository.find(100L);
dateTimeEntityRepository.clean();
}
}

View File

@ -2,8 +2,8 @@
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
<persistence-unit name="java-jpa-scheduled-day">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
@ -66,4 +66,22 @@
</properties>
</persistence-unit>
<persistence-unit name="java8-datetime-postgresql" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.baeldung.jpa.datetime.JPA22DateTimeEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/java8-datetime2"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="postgres"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<!-- configure logging -->
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>