From 3b196fb2e1cea0e4d76e646175ee71e5f9e466a3 Mon Sep 17 00:00:00 2001 From: Yordan Gigov Date: Thu, 13 Oct 2016 10:19:30 +0300 Subject: [PATCH] HHH-11171 - Java 8 time types fail Externalization --- .../mapping/basic/InstantLiteralTest.java | 93 +++++++++++++++++++ .../java/InstantJavaDescriptor.java | 6 +- .../java/LocalDateJavaDescriptor.java | 2 +- .../java/LocalDateTimeJavaDescriptor.java | 2 +- .../java/LocalTimeJavaDescriptor.java | 2 +- .../java/OffsetDateTimeJavaDescriptor.java | 2 +- .../java/OffsetTimeJavaDescriptor.java | 2 +- .../java/ZonedDateTimeJavaDescriptor.java | 2 +- .../java/DurationDescriptorTest.java | 34 +++++++ .../java/InstantDescriptorTest.java | 34 +++++++ .../java/LocalDateDescriptorTest.java | 34 +++++++ .../java/LocalDateTimeDescriptorTest.java | 34 +++++++ .../java/LocalTimeDescriptorTest.java | 34 +++++++ .../java/OffsetDateTimeDescriptorTest.java | 36 +++++++ .../java/OffsetTimeDescriptorTest.java | 36 +++++++ .../java/ZonedDateTimeDescriptorTest.java | 36 +++++++ 16 files changed, 380 insertions(+), 9 deletions(-) create mode 100644 documentation/src/test/java/org/hibernate/userguide/mapping/basic/InstantLiteralTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/DurationDescriptorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/InstantDescriptorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalDateDescriptorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalDateTimeDescriptorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalTimeDescriptorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/OffsetDateTimeDescriptorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/OffsetTimeDescriptorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/ZonedDateTimeDescriptorTest.java diff --git a/documentation/src/test/java/org/hibernate/userguide/mapping/basic/InstantLiteralTest.java b/documentation/src/test/java/org/hibernate/userguide/mapping/basic/InstantLiteralTest.java new file mode 100644 index 0000000000..fd3df8676a --- /dev/null +++ b/documentation/src/test/java/org/hibernate/userguide/mapping/basic/InstantLiteralTest.java @@ -0,0 +1,93 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.userguide.mapping.basic; + +import java.time.Instant; +import java.time.format.DateTimeFormatter; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.hibernate.Session; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Vlad Mihalcea + */ +public class InstantLiteralTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + DateEvent.class + }; + } + + @Test + public void test() { + final DateEvent _dateEvent = doInJPA( this::entityManagerFactory, entityManager -> { + DateEvent dateEvent = new DateEvent(); + dateEvent.setCreatedOn( Instant.from( DateTimeFormatter.ISO_INSTANT.parse( "2016-10-13T06:40:18.745Z" ) ) ); + entityManager.persist( dateEvent ); + + return dateEvent; + } ); + doInJPA( this::entityManagerFactory, entityManager -> { + DateEvent dateEvent = entityManager.unwrap( Session.class ) + .createQuery( + "select de " + + "from DateEvent de " + + "where de.createdOn = '2016-10-13T06:40:18.745Z' ", DateEvent.class ) + .getSingleResult(); + + assertNotNull(dateEvent); + assertEquals(_dateEvent.getId(), dateEvent.getId()); + } ); + doInJPA( this::entityManagerFactory, entityManager -> { + DateEvent dateEvent = entityManager.unwrap( Session.class ) + .createQuery( + "select de " + + "from DateEvent de " + + "where de.createdOn = :createdOn ", DateEvent.class ) + .setParameter( "createdOn", Instant.from( DateTimeFormatter.ISO_INSTANT.parse( "2016-10-13T06:40:18.745Z" ) ) ) + .getSingleResult(); + + assertNotNull(dateEvent); + assertEquals(_dateEvent.getId(), dateEvent.getId()); + } ); + } + + @Entity(name = "DateEvent") + public static class DateEvent { + + @Id + @GeneratedValue + private Long id; + + @Column + private Instant createdOn; + + public Long getId() { + return id; + } + + public Instant getCreatedOn() { + return createdOn; + } + + public void setCreatedOn(Instant createdOn) { + this.createdOn = createdOn; + } + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaDescriptor.java index f5bbab3733..bdd0659b4a 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaDescriptor.java @@ -11,11 +11,11 @@ import java.time.Instant; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; -import org.hibernate.type.InstantType; import org.hibernate.type.descriptor.WrapperOptions; /** @@ -36,12 +36,12 @@ public class InstantJavaDescriptor extends AbstractTypeDescriptor { @Override public String toString(Instant value) { - return InstantType.FORMATTER.format( ZonedDateTime.ofInstant( value, ZoneId.of( "UTC" ) ) ); + return DateTimeFormatter.ISO_INSTANT.format( value ); } @Override public Instant fromString(String string) { - return (Instant) InstantType.FORMATTER.parse( string ); + return Instant.from( DateTimeFormatter.ISO_INSTANT.parse( string ) ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaDescriptor.java index d911e0fb04..61f9c3458d 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaDescriptor.java @@ -42,7 +42,7 @@ public class LocalDateJavaDescriptor extends AbstractTypeDescriptor { @Override public LocalDate fromString(String string) { - return (LocalDate) LocalDateType.FORMATTER.parse( string ); + return LocalDate.from( LocalDateType.FORMATTER.parse( string ) ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaDescriptor.java index 85596b75e0..1760393c25 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaDescriptor.java @@ -40,7 +40,7 @@ public class LocalDateTimeJavaDescriptor extends AbstractTypeDescriptor { @Override public LocalTime fromString(String string) { - return (LocalTime) LocalTimeType.FORMATTER.parse( string ); + return LocalTime.from( LocalTimeType.FORMATTER.parse( string ) ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OffsetDateTimeJavaDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OffsetDateTimeJavaDescriptor.java index 6a358f1eeb..df36984295 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OffsetDateTimeJavaDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OffsetDateTimeJavaDescriptor.java @@ -40,7 +40,7 @@ public class OffsetDateTimeJavaDescriptor extends AbstractTypeDescriptor @Override public OffsetTime fromString(String string) { - return (OffsetTime) OffsetTimeType.FORMATTER.parse( string ); + return OffsetTime.from( OffsetTimeType.FORMATTER.parse( string ) ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZonedDateTimeJavaDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZonedDateTimeJavaDescriptor.java index 021b239ea2..26cdd1e410 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZonedDateTimeJavaDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZonedDateTimeJavaDescriptor.java @@ -40,7 +40,7 @@ public class ZonedDateTimeJavaDescriptor extends AbstractTypeDescriptor. + */ +package org.hibernate.test.type.descriptor.java; + +import java.time.Duration; +import org.hibernate.type.descriptor.java.DurationJavaDescriptor; + +/** + * @author Jordan Gigov + */ +public class DurationDescriptorTest extends AbstractDescriptorTest { + final Duration original = Duration.ofSeconds( 3621, 256); + final Duration copy = Duration.ofSeconds( 3621, 256); + final Duration different = Duration.ofSeconds( 1621, 156); + + public DurationDescriptorTest() { + super(DurationJavaDescriptor.INSTANCE); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/InstantDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/InstantDescriptorTest.java new file mode 100644 index 0000000000..6cb4420ea3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/InstantDescriptorTest.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.type.descriptor.java; + +import java.time.Instant; +import org.hibernate.type.descriptor.java.InstantJavaDescriptor; + +/** + * @author Jordan Gigov + */ +public class InstantDescriptorTest extends AbstractDescriptorTest { + final Instant original = Instant.ofEpochMilli( 1476340818745L ); + final Instant copy = Instant.ofEpochMilli( 1476340818745L ); + final Instant different = Instant.ofEpochMilli( 1476340818746L ); + + public InstantDescriptorTest() { + super(InstantJavaDescriptor.INSTANCE); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalDateDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalDateDescriptorTest.java new file mode 100644 index 0000000000..a54fb5321f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalDateDescriptorTest.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.type.descriptor.java; + +import java.time.LocalDate; +import org.hibernate.type.descriptor.java.LocalDateJavaDescriptor; + +/** + * @author Jordan Gigov + */ +public class LocalDateDescriptorTest extends AbstractDescriptorTest { + final LocalDate original = LocalDate.of( 2016, 10, 8 ); + final LocalDate copy = LocalDate.of( 2016, 10, 8 ); + final LocalDate different = LocalDate.of( 2013, 8, 8 ); + + public LocalDateDescriptorTest() { + super(LocalDateJavaDescriptor.INSTANCE); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalDateTimeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalDateTimeDescriptorTest.java new file mode 100644 index 0000000000..010c0f0a27 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalDateTimeDescriptorTest.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.type.descriptor.java; + +import java.time.LocalDateTime; +import org.hibernate.type.descriptor.java.LocalDateTimeJavaDescriptor; + +/** + * @author Jordan Gigov + */ +public class LocalDateTimeDescriptorTest extends AbstractDescriptorTest { + final LocalDateTime original = LocalDateTime.of( 2016, 10, 8, 10, 15, 0 ); + final LocalDateTime copy = LocalDateTime.of( 2016, 10, 8, 10, 15, 0 ); + final LocalDateTime different = LocalDateTime.of( 2013, 8, 8, 15, 12 ); + + public LocalDateTimeDescriptorTest() { + super(LocalDateTimeJavaDescriptor.INSTANCE); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalTimeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalTimeDescriptorTest.java new file mode 100644 index 0000000000..805bcbafe5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/LocalTimeDescriptorTest.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.type.descriptor.java; + +import java.time.LocalDate; +import org.hibernate.type.descriptor.java.LocalDateJavaDescriptor; + +/** + * @author Jordan Gigov + */ +public class LocalTimeDescriptorTest extends AbstractDescriptorTest { + final LocalDate original = LocalDate.of( 2016, 10, 8 ); + final LocalDate copy = LocalDate.of( 2016, 10, 8 ); + final LocalDate different = LocalDate.of( 2013, 8, 8 ); + + public LocalTimeDescriptorTest() { + super(LocalDateJavaDescriptor.INSTANCE); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/OffsetDateTimeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/OffsetDateTimeDescriptorTest.java new file mode 100644 index 0000000000..4c675a2bda --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/OffsetDateTimeDescriptorTest.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.type.descriptor.java; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import org.hibernate.type.descriptor.java.OffsetDateTimeJavaDescriptor; + +/** + * @author Jordan Gigov + */ +public class OffsetDateTimeDescriptorTest extends AbstractDescriptorTest { + final OffsetDateTime original = OffsetDateTime.of(LocalDateTime.of( 2016, 10, 8, 15, 13 ), ZoneOffset.ofHoursMinutes( 2, 0)); + final OffsetDateTime copy = OffsetDateTime.of(LocalDateTime.of( 2016, 10, 8, 15, 13 ), ZoneOffset.ofHoursMinutes( 2, 0)); + final OffsetDateTime different = OffsetDateTime.of(LocalDateTime.of( 2016, 10, 8, 15, 13 ), ZoneOffset.ofHoursMinutes( 4, 30)); + + public OffsetDateTimeDescriptorTest() { + super(OffsetDateTimeJavaDescriptor.INSTANCE); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/OffsetTimeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/OffsetTimeDescriptorTest.java new file mode 100644 index 0000000000..3f9f0c7e74 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/OffsetTimeDescriptorTest.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.type.descriptor.java; + +import java.time.LocalTime; +import java.time.OffsetTime; +import java.time.ZoneOffset; +import org.hibernate.type.descriptor.java.OffsetTimeJavaDescriptor; + +/** + * @author Jordan Gigov + */ +public class OffsetTimeDescriptorTest extends AbstractDescriptorTest { + final OffsetTime original = OffsetTime.of(LocalTime.of( 15, 13 ), ZoneOffset.ofHoursMinutes( -6, 0)); + final OffsetTime copy = OffsetTime.of(LocalTime.of( 15, 13 ), ZoneOffset.ofHoursMinutes( -6, 0)); + final OffsetTime different = OffsetTime.of(LocalTime.of( 15, 13 ), ZoneOffset.ofHoursMinutes( 4, 30)); + + public OffsetTimeDescriptorTest() { + super(OffsetTimeJavaDescriptor.INSTANCE); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/ZonedDateTimeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/ZonedDateTimeDescriptorTest.java new file mode 100644 index 0000000000..1e5d69c9e5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/ZonedDateTimeDescriptorTest.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.type.descriptor.java; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import org.hibernate.type.descriptor.java.ZonedDateTimeJavaDescriptor; + +/** + * @author Jordan Gigov + */ +public class ZonedDateTimeDescriptorTest extends AbstractDescriptorTest { + final ZonedDateTime original = ZonedDateTime.of( LocalDateTime.of( 2016, 10, 8, 15, 13 ), ZoneId.of( "UTC" )); + final ZonedDateTime copy = ZonedDateTime.of( LocalDateTime.of( 2016, 10, 8, 15, 13 ), ZoneId.of( "UTC" ) ); + final ZonedDateTime different = ZonedDateTime.of( LocalDateTime.of( 2016, 10, 8, 15, 13 ), ZoneId.of( "EET" ) ); + + public ZonedDateTimeDescriptorTest() { + super(ZonedDateTimeJavaDescriptor.INSTANCE); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + +}