HHH-11171 - Java 8 time types fail Externalization

This commit is contained in:
Yordan Gigov 2016-10-13 10:19:30 +03:00 committed by Vlad Mihalcea
parent 2ecfd488b7
commit 3b196fb2e1
16 changed files with 380 additions and 9 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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<Instant> {
@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

View File

@ -42,7 +42,7 @@ public class LocalDateJavaDescriptor extends AbstractTypeDescriptor<LocalDate> {
@Override
public LocalDate fromString(String string) {
return (LocalDate) LocalDateType.FORMATTER.parse( string );
return LocalDate.from( LocalDateType.FORMATTER.parse( string ) );
}
@Override

View File

@ -40,7 +40,7 @@ public class LocalDateTimeJavaDescriptor extends AbstractTypeDescriptor<LocalDat
@Override
public LocalDateTime fromString(String string) {
return (LocalDateTime) LocalDateTimeType.FORMATTER.parse( string );
return LocalDateTime.from( LocalDateTimeType.FORMATTER.parse( string ) );
}
@Override

View File

@ -44,7 +44,7 @@ public class LocalTimeJavaDescriptor extends AbstractTypeDescriptor<LocalTime> {
@Override
public LocalTime fromString(String string) {
return (LocalTime) LocalTimeType.FORMATTER.parse( string );
return LocalTime.from( LocalTimeType.FORMATTER.parse( string ) );
}
@Override

View File

@ -40,7 +40,7 @@ public class OffsetDateTimeJavaDescriptor extends AbstractTypeDescriptor<OffsetD
@Override
public OffsetDateTime fromString(String string) {
return (OffsetDateTime) OffsetDateTimeType.FORMATTER.parse( string );
return OffsetDateTime.from( OffsetDateTimeType.FORMATTER.parse( string ) );
}
@Override

View File

@ -44,7 +44,7 @@ public class OffsetTimeJavaDescriptor extends AbstractTypeDescriptor<OffsetTime>
@Override
public OffsetTime fromString(String string) {
return (OffsetTime) OffsetTimeType.FORMATTER.parse( string );
return OffsetTime.from( OffsetTimeType.FORMATTER.parse( string ) );
}
@Override

View File

@ -40,7 +40,7 @@ public class ZonedDateTimeJavaDescriptor extends AbstractTypeDescriptor<ZonedDat
@Override
public ZonedDateTime fromString(String string) {
return (ZonedDateTime) ZonedDateTimeType.FORMATTER.parse( string );
return ZonedDateTime.from( ZonedDateTimeType.FORMATTER.parse( string ) );
}
@Override

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Duration> {
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<Duration> getTestData() {
return new Data<>( original, copy, different );
}
@Override
protected boolean shouldBeMutable() {
return false;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Instant> {
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<Instant> getTestData() {
return new Data<>( original, copy, different );
}
@Override
protected boolean shouldBeMutable() {
return false;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<LocalDate> {
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<LocalDate> getTestData() {
return new Data<>( original, copy, different );
}
@Override
protected boolean shouldBeMutable() {
return false;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<LocalDateTime> {
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<LocalDateTime> getTestData() {
return new Data<>( original, copy, different );
}
@Override
protected boolean shouldBeMutable() {
return false;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<LocalDate> {
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<LocalDate> getTestData() {
return new Data<>( original, copy, different );
}
@Override
protected boolean shouldBeMutable() {
return false;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<OffsetDateTime> {
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<OffsetDateTime> getTestData() {
return new Data<>( original, copy, different );
}
@Override
protected boolean shouldBeMutable() {
return false;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<OffsetTime> {
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<OffsetTime> getTestData() {
return new Data<>( original, copy, different );
}
@Override
protected boolean shouldBeMutable() {
return false;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<ZonedDateTime> {
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<ZonedDateTime> getTestData() {
return new Data<>( original, copy, different );
}
@Override
protected boolean shouldBeMutable() {
return false;
}
}