HHH-10026 - Consider hibernate-java8 types for use with @Version

(cherry picked from commit 080014558d)
This commit is contained in:
Steve Ebersole 2015-08-31 13:07:18 -05:00
parent f6879787e2
commit c9a8468056
7 changed files with 366 additions and 60 deletions

View File

@ -8,12 +8,9 @@ package org.hibernate.type;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Locale; import java.util.Locale;
import org.hibernate.dialect.Dialect; 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.java.LocalDateJavaDescriptor;
import org.hibernate.type.descriptor.sql.DateTypeDescriptor; import org.hibernate.type.descriptor.sql.DateTypeDescriptor;
@ -22,7 +19,7 @@ import org.hibernate.type.descriptor.sql.DateTypeDescriptor;
*/ */
public class LocalDateType public class LocalDateType
extends AbstractSingleColumnStandardBasicType<LocalDate> extends AbstractSingleColumnStandardBasicType<LocalDate>
implements VersionType<LocalDate>, LiteralType<LocalDate> { implements LiteralType<LocalDate> {
/** /**
* Singleton access * Singleton access
@ -49,20 +46,4 @@ public class LocalDateType
public String objectToSQLString(LocalDate value, Dialect dialect) throws Exception { public String objectToSQLString(LocalDate value, Dialect dialect) throws Exception {
return "{d '" + FORMATTER.format( value ) + "'}"; 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<LocalDate> getComparator() {
return ComparableComparator.INSTANCE;
}
} }

View File

@ -8,12 +8,9 @@ package org.hibernate.type;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Locale; import java.util.Locale;
import org.hibernate.dialect.Dialect; 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.java.LocalTimeJavaDescriptor;
import org.hibernate.type.descriptor.sql.TimeTypeDescriptor; import org.hibernate.type.descriptor.sql.TimeTypeDescriptor;
@ -24,7 +21,7 @@ import org.hibernate.type.descriptor.sql.TimeTypeDescriptor;
*/ */
public class LocalTimeType public class LocalTimeType
extends AbstractSingleColumnStandardBasicType<LocalTime> extends AbstractSingleColumnStandardBasicType<LocalTime>
implements VersionType<LocalTime>, LiteralType<LocalTime> { implements LiteralType<LocalTime> {
/** /**
* Singleton access * Singleton access
*/ */
@ -50,20 +47,4 @@ public class LocalTimeType
public String objectToSQLString(LocalTime value, Dialect dialect) throws Exception { public String objectToSQLString(LocalTime value, Dialect dialect) throws Exception {
return "{t '" + FORMATTER.format( value ) + "'}"; 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<LocalTime> getComparator() {
return ComparableComparator.INSTANCE;
}
} }

View File

@ -8,12 +8,9 @@ package org.hibernate.type;
import java.time.OffsetTime; import java.time.OffsetTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Locale; import java.util.Locale;
import org.hibernate.dialect.Dialect; 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.java.OffsetTimeJavaDescriptor;
import org.hibernate.type.descriptor.sql.TimeTypeDescriptor; import org.hibernate.type.descriptor.sql.TimeTypeDescriptor;
@ -22,7 +19,7 @@ import org.hibernate.type.descriptor.sql.TimeTypeDescriptor;
*/ */
public class OffsetTimeType public class OffsetTimeType
extends AbstractSingleColumnStandardBasicType<OffsetTime> extends AbstractSingleColumnStandardBasicType<OffsetTime>
implements VersionType<OffsetTime>, LiteralType<OffsetTime> { implements LiteralType<OffsetTime> {
/** /**
* Singleton access * Singleton access
@ -40,22 +37,6 @@ public class OffsetTimeType
return "{t '" + FORMATTER.format( value ) + "'}"; 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<OffsetTime> getComparator() {
return ComparableComparator.INSTANCE;
}
@Override @Override
public String getName() { public String getName() {
return OffsetTime.class.getSimpleName(); return OffsetTime.class.getSimpleName();

View File

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

View File

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

View File

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

View File

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