From 2432527980d6adcd5fe77cf11857a45945b27a03 Mon Sep 17 00:00:00 2001 From: eelhazati Date: Thu, 10 Jan 2019 16:22:07 +0100 Subject: [PATCH 1/4] jpa 2.2 support for java 8 date and time types. --- persistence-modules/java-jpa/pom.xml | 31 ++- .../datetime/DateTimeEntityRepository.java | 68 +++++++ .../jpa/datetime/JPA22DateTimeEntity.java | 176 ++++++++++++++++++ .../com/baeldung/jpa/datetime/MainApp.java | 18 ++ .../main/resources/META-INF/persistence.xml | 22 ++- 5 files changed, 310 insertions(+), 5 deletions(-) create mode 100644 persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/DateTimeEntityRepository.java create mode 100644 persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/JPA22DateTimeEntity.java create mode 100644 persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/MainApp.java diff --git a/persistence-modules/java-jpa/pom.xml b/persistence-modules/java-jpa/pom.xml index ddab51a2e2..7e9c7b83f1 100644 --- a/persistence-modules/java-jpa/pom.xml +++ b/persistence-modules/java-jpa/pom.xml @@ -4,8 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 java-jpa - java-jpa - + java-jpa + parent-modules com.baeldung @@ -24,10 +24,35 @@ h2 ${h2.version} + + + + javax.persistence + javax.persistence-api + 2.2 + + + + + org.eclipse.persistence + eclipselink + ${eclipselink.version} + runtime + + + org.postgresql + postgresql + ${postgres.version} + runtime + bundle + - 5.3.1.Final + 5.4.0.Final 1.4.197 + 2.7.4-RC1 + 42.2.5 + \ No newline at end of file diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/DateTimeEntityRepository.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/DateTimeEntityRepository.java new file mode 100644 index 0000000000..0bd04da221 --- /dev/null +++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/DateTimeEntityRepository.java @@ -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(); + } +} diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/JPA22DateTimeEntity.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/JPA22DateTimeEntity.java new file mode 100644 index 0000000000..065385bd86 --- /dev/null +++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/JPA22DateTimeEntity.java @@ -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; + } +} diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/MainApp.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/MainApp.java new file mode 100644 index 0000000000..7f23f44254 --- /dev/null +++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/datetime/MainApp.java @@ -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(); + } + +} \ No newline at end of file diff --git a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml index 433d456cc9..9e4f4bf4c9 100644 --- a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml @@ -2,8 +2,8 @@ + http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" + version="2.2"> org.hibernate.jpa.HibernatePersistenceProvider @@ -66,4 +66,22 @@ + + org.eclipse.persistence.jpa.PersistenceProvider + com.baeldung.jpa.datetime.JPA22DateTimeEntity + true + + + + + + + + + + + + + + \ No newline at end of file From 3907f624b57bf9314ec8a9bf73d354685ff50a80 Mon Sep 17 00:00:00 2001 From: eelhazati Date: Thu, 10 Jan 2019 16:36:52 +0100 Subject: [PATCH 2/4] jpa 2.2 support for java 8 date and time types. --- persistence-modules/java-jpa/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/persistence-modules/java-jpa/pom.xml b/persistence-modules/java-jpa/pom.xml index 7e9c7b83f1..fa47d6bd9c 100644 --- a/persistence-modules/java-jpa/pom.xml +++ b/persistence-modules/java-jpa/pom.xml @@ -44,7 +44,6 @@ postgresql ${postgres.version} runtime - bundle From 1c6dba97187e598e25219291408b87be6dd8e793 Mon Sep 17 00:00:00 2001 From: eelhazati Date: Thu, 10 Jan 2019 16:50:29 +0100 Subject: [PATCH 3/4] excludes jpa entities. --- .../java-jpa/src/main/resources/META-INF/persistence.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml index 9e4f4bf4c9..f7258e3109 100644 --- a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml @@ -8,6 +8,8 @@ org.hibernate.jpa.HibernatePersistenceProvider com.baeldung.sqlresultsetmapping.ScheduledDay + com.baeldung.sqlresultsetmapping.Employee + true Date: Thu, 10 Jan 2019 17:08:47 +0100 Subject: [PATCH 4/4] exludes jpa entities. --- .../java-jpa/src/main/resources/META-INF/persistence.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml index f7258e3109..8592fce533 100644 --- a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml @@ -26,6 +26,7 @@ org.hibernate.jpa.HibernatePersistenceProvider com.baeldung.jpa.stringcast.Message + true @@ -41,6 +42,7 @@ org.hibernate.jpa.HibernatePersistenceProvider com.baeldung.jpa.model.Car + true