From c9a8468056a25a7e17bb98b531323fad600f4f21 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 31 Aug 2015 13:07:18 -0500 Subject: [PATCH] HHH-10026 - Consider hibernate-java8 types for use with @Version (cherry picked from commit 080014558dff3ec174a14f35ef9ce10506783f86) --- .../org/hibernate/type/LocalDateType.java | 21 +---- .../org/hibernate/type/LocalTimeType.java | 21 +---- .../org/hibernate/type/OffsetTimeType.java | 21 +---- .../test/version/InstantVersionTest.java | 90 ++++++++++++++++++ .../version/LocalDateTimeVersionTest.java | 91 +++++++++++++++++++ .../version/OffsetDateTimeVersionTest.java | 91 +++++++++++++++++++ .../version/ZonedDateTimeVersionTest.java | 91 +++++++++++++++++++ 7 files changed, 366 insertions(+), 60 deletions(-) create mode 100644 hibernate-java8/src/test/java/org/hibernate/test/version/InstantVersionTest.java create mode 100644 hibernate-java8/src/test/java/org/hibernate/test/version/LocalDateTimeVersionTest.java create mode 100644 hibernate-java8/src/test/java/org/hibernate/test/version/OffsetDateTimeVersionTest.java create mode 100644 hibernate-java8/src/test/java/org/hibernate/test/version/ZonedDateTimeVersionTest.java diff --git a/hibernate-java8/src/main/java/org/hibernate/type/LocalDateType.java b/hibernate-java8/src/main/java/org/hibernate/type/LocalDateType.java index 76dadbce4b..920401a2fa 100644 --- a/hibernate-java8/src/main/java/org/hibernate/type/LocalDateType.java +++ b/hibernate-java8/src/main/java/org/hibernate/type/LocalDateType.java @@ -8,12 +8,9 @@ package org.hibernate.type; import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.Comparator; import java.util.Locale; import org.hibernate.dialect.Dialect; -import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.type.descriptor.java.LocalDateJavaDescriptor; import org.hibernate.type.descriptor.sql.DateTypeDescriptor; @@ -22,7 +19,7 @@ import org.hibernate.type.descriptor.sql.DateTypeDescriptor; */ public class LocalDateType extends AbstractSingleColumnStandardBasicType - implements VersionType, LiteralType { + implements LiteralType { /** * Singleton access @@ -49,20 +46,4 @@ public class LocalDateType public String objectToSQLString(LocalDate value, Dialect dialect) throws Exception { return "{d '" + FORMATTER.format( value ) + "'}"; } - - @Override - public LocalDate seed(SessionImplementor session) { - return LocalDate.now(); - } - - @Override - public LocalDate next(LocalDate current, SessionImplementor session) { - return LocalDate.now(); - } - - @Override - @SuppressWarnings("unchecked") - public Comparator getComparator() { - return ComparableComparator.INSTANCE; - } } diff --git a/hibernate-java8/src/main/java/org/hibernate/type/LocalTimeType.java b/hibernate-java8/src/main/java/org/hibernate/type/LocalTimeType.java index b645ff0dc4..a9454a7d21 100644 --- a/hibernate-java8/src/main/java/org/hibernate/type/LocalTimeType.java +++ b/hibernate-java8/src/main/java/org/hibernate/type/LocalTimeType.java @@ -8,12 +8,9 @@ package org.hibernate.type; import java.time.LocalTime; import java.time.format.DateTimeFormatter; -import java.util.Comparator; import java.util.Locale; import org.hibernate.dialect.Dialect; -import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.type.descriptor.java.LocalTimeJavaDescriptor; import org.hibernate.type.descriptor.sql.TimeTypeDescriptor; @@ -24,7 +21,7 @@ import org.hibernate.type.descriptor.sql.TimeTypeDescriptor; */ public class LocalTimeType extends AbstractSingleColumnStandardBasicType - implements VersionType, LiteralType { + implements LiteralType { /** * Singleton access */ @@ -50,20 +47,4 @@ public class LocalTimeType public String objectToSQLString(LocalTime value, Dialect dialect) throws Exception { return "{t '" + FORMATTER.format( value ) + "'}"; } - - @Override - public LocalTime seed(SessionImplementor session) { - return LocalTime.now(); - } - - @Override - public LocalTime next(LocalTime current, SessionImplementor session) { - return LocalTime.now(); - } - - @Override - @SuppressWarnings("unchecked") - public Comparator getComparator() { - return ComparableComparator.INSTANCE; - } } diff --git a/hibernate-java8/src/main/java/org/hibernate/type/OffsetTimeType.java b/hibernate-java8/src/main/java/org/hibernate/type/OffsetTimeType.java index 7c9796fc9f..d620826933 100644 --- a/hibernate-java8/src/main/java/org/hibernate/type/OffsetTimeType.java +++ b/hibernate-java8/src/main/java/org/hibernate/type/OffsetTimeType.java @@ -8,12 +8,9 @@ package org.hibernate.type; import java.time.OffsetTime; import java.time.format.DateTimeFormatter; -import java.util.Comparator; import java.util.Locale; import org.hibernate.dialect.Dialect; -import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.type.descriptor.java.OffsetTimeJavaDescriptor; import org.hibernate.type.descriptor.sql.TimeTypeDescriptor; @@ -22,7 +19,7 @@ import org.hibernate.type.descriptor.sql.TimeTypeDescriptor; */ public class OffsetTimeType extends AbstractSingleColumnStandardBasicType - implements VersionType, LiteralType { + implements LiteralType { /** * Singleton access @@ -40,22 +37,6 @@ public class OffsetTimeType return "{t '" + FORMATTER.format( value ) + "'}"; } - @Override - public OffsetTime seed(SessionImplementor session) { - return OffsetTime.now(); - } - - @Override - public OffsetTime next(OffsetTime current, SessionImplementor session) { - return OffsetTime.now(); - } - - @Override - @SuppressWarnings("unchecked") - public Comparator getComparator() { - return ComparableComparator.INSTANCE; - } - @Override public String getName() { return OffsetTime.class.getSimpleName(); diff --git a/hibernate-java8/src/test/java/org/hibernate/test/version/InstantVersionTest.java b/hibernate-java8/src/test/java/org/hibernate/test/version/InstantVersionTest.java new file mode 100644 index 0000000000..a9531eaabc --- /dev/null +++ b/hibernate-java8/src/test/java/org/hibernate/test/version/InstantVersionTest.java @@ -0,0 +1,90 @@ +/* + * 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.version; + +import java.time.Instant; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Version; + +import org.hibernate.Session; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-10026" ) +public class InstantVersionTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { TheEntity.class }; + } + + @Test + public void testInstantUsageAsVersion() { + Session session = openSession(); + session.getTransaction().begin(); + TheEntity e = new TheEntity( 1 ); + session.save( e ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.getTransaction().begin(); + e = session.byId( TheEntity.class ).load( 1 ); + assertThat( e.getTs(), notNullValue() ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.getTransaction().begin(); + e = session.byId( TheEntity.class ).load( 1 ); + session.delete( e ); + session.getTransaction().commit(); + session.close(); + } + + + @Entity(name = "TheEntity") + @Table(name="the_entity") + public static class TheEntity { + private Integer id; + private Instant ts; + + public TheEntity() { + } + + public TheEntity(Integer id) { + this.id = id; + } + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Version + public Instant getTs() { + return ts; + } + + public void setTs(Instant ts) { + this.ts = ts; + } + } +} diff --git a/hibernate-java8/src/test/java/org/hibernate/test/version/LocalDateTimeVersionTest.java b/hibernate-java8/src/test/java/org/hibernate/test/version/LocalDateTimeVersionTest.java new file mode 100644 index 0000000000..de3a8f5761 --- /dev/null +++ b/hibernate-java8/src/test/java/org/hibernate/test/version/LocalDateTimeVersionTest.java @@ -0,0 +1,91 @@ +/* + * 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.version; + +import java.time.LocalDateTime; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Version; + +import org.hibernate.Session; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-10026" ) +public class LocalDateTimeVersionTest extends BaseNonConfigCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { TheEntity.class }; + } + + @Test + public void testInstantUsageAsVersion() { + Session session = openSession(); + session.getTransaction().begin(); + TheEntity e = new TheEntity( 1 ); + session.save( e ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.getTransaction().begin(); + e = session.byId( TheEntity.class ).load( 1 ); + assertThat( e.getTs(), notNullValue() ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.getTransaction().begin(); + e = session.byId( TheEntity.class ).load( 1 ); + session.delete( e ); + session.getTransaction().commit(); + session.close(); + } + + + @Entity(name = "TheEntity") + @Table(name="the_entity") + public static class TheEntity { + private Integer id; + private LocalDateTime ts; + + public TheEntity() { + } + + public TheEntity(Integer id) { + this.id = id; + } + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Version + public LocalDateTime getTs() { + return ts; + } + + public void setTs(LocalDateTime ts) { + this.ts = ts; + } + } +} diff --git a/hibernate-java8/src/test/java/org/hibernate/test/version/OffsetDateTimeVersionTest.java b/hibernate-java8/src/test/java/org/hibernate/test/version/OffsetDateTimeVersionTest.java new file mode 100644 index 0000000000..ce309f2293 --- /dev/null +++ b/hibernate-java8/src/test/java/org/hibernate/test/version/OffsetDateTimeVersionTest.java @@ -0,0 +1,91 @@ +/* + * 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.version; + +import java.time.OffsetDateTime; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Version; + +import org.hibernate.Session; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-10026" ) +public class OffsetDateTimeVersionTest extends BaseNonConfigCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { TheEntity.class }; + } + + @Test + public void testInstantUsageAsVersion() { + Session session = openSession(); + session.getTransaction().begin(); + TheEntity e = new TheEntity( 1 ); + session.save( e ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.getTransaction().begin(); + e = session.byId( TheEntity.class ).load( 1 ); + assertThat( e.getTs(), notNullValue() ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.getTransaction().begin(); + e = session.byId( TheEntity.class ).load( 1 ); + session.delete( e ); + session.getTransaction().commit(); + session.close(); + } + + + @Entity(name = "TheEntity") + @Table(name="the_entity") + public static class TheEntity { + private Integer id; + private OffsetDateTime ts; + + public TheEntity() { + } + + public TheEntity(Integer id) { + this.id = id; + } + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Version + public OffsetDateTime getTs() { + return ts; + } + + public void setTs(OffsetDateTime ts) { + this.ts = ts; + } + } +} diff --git a/hibernate-java8/src/test/java/org/hibernate/test/version/ZonedDateTimeVersionTest.java b/hibernate-java8/src/test/java/org/hibernate/test/version/ZonedDateTimeVersionTest.java new file mode 100644 index 0000000000..0c1e45b22a --- /dev/null +++ b/hibernate-java8/src/test/java/org/hibernate/test/version/ZonedDateTimeVersionTest.java @@ -0,0 +1,91 @@ +/* + * 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.version; + +import java.time.ZonedDateTime; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Version; + +import org.hibernate.Session; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-10026" ) +public class ZonedDateTimeVersionTest extends BaseNonConfigCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { TheEntity.class }; + } + + @Test + public void testInstantUsageAsVersion() { + Session session = openSession(); + session.getTransaction().begin(); + TheEntity e = new TheEntity( 1 ); + session.save( e ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.getTransaction().begin(); + e = session.byId( TheEntity.class ).load( 1 ); + assertThat( e.getTs(), notNullValue() ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.getTransaction().begin(); + e = session.byId( TheEntity.class ).load( 1 ); + session.delete( e ); + session.getTransaction().commit(); + session.close(); + } + + + @Entity(name = "TheEntity") + @Table(name="the_entity") + public static class TheEntity { + private Integer id; + private ZonedDateTime ts; + + public TheEntity() { + } + + public TheEntity(Integer id) { + this.id = id; + } + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Version + public ZonedDateTime getTs() { + return ts; + } + + public void setTs(ZonedDateTime ts) { + this.ts = ts; + } + } +}