From 664950367d790a7ccd0888f675328afc4a16ab7f Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Tue, 21 Jun 2016 00:38:30 -0700 Subject: [PATCH] HHH-8999 : Added test cases (cherry picked from commit 280f93add3973595383a8a4af15d35f7f2e2ae8a) --- .../test/id/array/ByteArrayIdTest.java | 132 +++++++++++ .../test/id/array/CharacterArrayIdTest.java | 132 +++++++++++ .../id/array/PrimitiveByteArrayIdTest.java | 132 +++++++++++ .../array/PrimitiveCharacterArrayIdTest.java | 132 +++++++++++ .../id/usertype/UserTypeComparableIdTest.java | 213 ++++++++++++++++++ .../usertype/UserTypeNonComparableIdTest.java | 199 ++++++++++++++++ 6 files changed, 940 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/array/ByteArrayIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/array/CharacterArrayIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveByteArrayIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveCharacterArrayIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeComparableIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeNonComparableIdTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/array/ByteArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/array/ByteArrayIdTest.java new file mode 100644 index 0000000000..d19277a3f1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/array/ByteArrayIdTest.java @@ -0,0 +1,132 @@ +/* + * 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.id.array; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Piotr Krauzowicz + * @author Gail Badner + */ +public class ByteArrayIdTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { DemoEntity.class }; + } + + @Before + public void prepare() { + Session s = openSession(); + s.getTransaction().begin(); + for ( int i = 0; i < 3; i++ ) { + DemoEntity entity = new DemoEntity(); + entity.id = new Byte[] { + (byte) ( i + 1 ), + (byte) ( i + 2 ), + (byte) ( i + 3 ), + (byte) ( i + 4 ) + }; + entity.name = "Simple name " + i; + s.persist( entity ); + } + s.getTransaction().commit(); + s.close(); + } + + @After + public void cleanup() { + Session s = openSession(); + s.getTransaction().begin(); + s.createQuery( "delete from ByteArrayIdTest$DemoEntity" ).executeUpdate(); + s.getTransaction().commit(); + s.close(); + } + + /** + * Removes two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testMultipleDeletions() { + Session s = openSession(); + s.getTransaction().begin(); + Query query = s.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); + List results = query.list(); + s.delete( results.get( 0 ) ); + s.delete( results.get( 1 ) ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + query = s.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); + assertEquals( 1, query.list().size() ); + s.getTransaction().commit(); + s.close(); + } + + /** + * Updates two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testMultipleUpdates() { + Session s = openSession(); + s.getTransaction().begin(); + Query query = s.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + results.get( 0 ).name = "Different 0"; + results.get( 1 ).name = "Different 1"; + final String lastResultName = results.get( 0 ).name; + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + query = s.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); + results = (List) query.list(); + final Set names = new HashSet( ); + for ( DemoEntity entity : results ) { + names.add( entity.name ); + } + assertTrue( names.contains( "Different 0" ) ); + assertTrue( names.contains( "Different 1" ) ); + assertTrue( names.contains( lastResultName ) ); + s.getTransaction().commit(); + s.close(); + } + + + @Entity + @Table(name="DemoEntity") + public static class DemoEntity { + @Id + public Byte[] id; + public String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/array/CharacterArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/array/CharacterArrayIdTest.java new file mode 100644 index 0000000000..f68206de7e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/array/CharacterArrayIdTest.java @@ -0,0 +1,132 @@ +/* + * 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.id.array; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Piotr Krauzowicz + * @author Gail Badner + */ +public class CharacterArrayIdTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { DemoEntity.class }; + } + + @Before + public void prepare() { + Session s = openSession(); + s.getTransaction().begin(); + for ( int i = 0; i < 3; i++ ) { + DemoEntity entity = new DemoEntity(); + entity.id = new Character[] { + (char) ( i + 1 ), + (char) ( i + 2 ), + (char) ( i + 3 ), + (char) ( i + 4 ) + }; + entity.name = "Simple name " + i; + s.persist( entity ); + } + s.getTransaction().commit(); + s.close(); + } + + @After + public void cleanup() { + Session s = openSession(); + s.getTransaction().begin(); + s.createQuery( "delete from CharacterArrayIdTest$DemoEntity" ).executeUpdate(); + s.getTransaction().commit(); + s.close(); + } + + /** + * Removes two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testMultipleDeletions() { + Session s = openSession(); + s.getTransaction().begin(); + Query query = s.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); + List results = query.list(); + s.delete( results.get( 0 ) ); + s.delete( results.get( 1 ) ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + query = s.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); + assertEquals( 1, query.list().size() ); + s.getTransaction().commit(); + s.close(); + } + + /** + * Updates two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testMultipleUpdates() { + Session s = openSession(); + s.getTransaction().begin(); + Query query = s.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + results.get( 0 ).name = "Different 0"; + results.get( 1 ).name = "Different 1"; + final String lastResultName = results.get( 0 ).name; + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + query = s.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); + results = (List) query.list(); + final Set names = new HashSet( ); + for ( DemoEntity entity : results ) { + names.add( entity.name ); + } + assertTrue( names.contains( "Different 0" ) ); + assertTrue( names.contains( "Different 1" ) ); + assertTrue( names.contains( lastResultName ) ); + s.getTransaction().commit(); + s.close(); + } + + + @Entity + @Table(name="DemoEntity") + public static class DemoEntity { + @Id + public Character[] id; + public String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveByteArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveByteArrayIdTest.java new file mode 100644 index 0000000000..09b573fa52 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveByteArrayIdTest.java @@ -0,0 +1,132 @@ +/* + * 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.id.array; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Piotr Krauzowicz + * @author Gail Badner + */ +public class PrimitiveByteArrayIdTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { DemoEntity.class }; + } + + @Before + public void prepare() { + Session s = openSession(); + s.getTransaction().begin(); + for ( int i = 0; i < 3; i++ ) { + DemoEntity entity = new DemoEntity(); + entity.id = new byte[] { + (byte) ( i + 1 ), + (byte) ( i + 2 ), + (byte) ( i + 3 ), + (byte) ( i + 4 ) + }; + entity.name = "Simple name " + i; + s.persist( entity ); + } + s.getTransaction().commit(); + s.close(); + } + + @After + public void cleanup() { + Session s = openSession(); + s.getTransaction().begin(); + s.createQuery( "delete from PrimitiveByteArrayIdTest$DemoEntity" ).executeUpdate(); + s.getTransaction().commit(); + s.close(); + } + + /** + * Removes two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testMultipleDeletions() { + Session s = openSession(); + s.getTransaction().begin(); + Query query = s.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); + List results = query.list(); + s.delete( results.get( 0 ) ); + s.delete( results.get( 1 ) ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + query = s.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); + assertEquals( 1, query.list().size() ); + s.getTransaction().commit(); + s.close(); + } + + /** + * Updates two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testMultipleUpdates() { + Session s = openSession(); + s.getTransaction().begin(); + Query query = s.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + results.get( 0 ).name = "Different 0"; + results.get( 1 ).name = "Different 1"; + final String lastResultName = results.get( 0 ).name; + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + query = s.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); + results = (List) query.list(); + final Set names = new HashSet( ); + for ( DemoEntity entity : results ) { + names.add( entity.name ); + } + assertTrue( names.contains( "Different 0" ) ); + assertTrue( names.contains( "Different 1" ) ); + assertTrue( names.contains( lastResultName ) ); + s.getTransaction().commit(); + s.close(); + } + + + @Entity + @Table(name="DemoEntity") + public static class DemoEntity { + @Id + public byte[] id; + public String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveCharacterArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveCharacterArrayIdTest.java new file mode 100644 index 0000000000..916060ecc9 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveCharacterArrayIdTest.java @@ -0,0 +1,132 @@ +/* + * 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.id.array; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Piotr Krauzowicz + * @author Gail Badner + */ +public class PrimitiveCharacterArrayIdTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { DemoEntity.class }; + } + + @Before + public void prepare() { + Session s = openSession(); + s.getTransaction().begin(); + for ( int i = 0; i < 3; i++ ) { + DemoEntity entity = new DemoEntity(); + entity.id = new char[] { + (char) ( i + 1 ), + (char) ( i + 2 ), + (char) ( i + 3 ), + (char) ( i + 4 ) + }; + entity.name = "Simple name " + i; + s.persist( entity ); + } + s.getTransaction().commit(); + s.close(); + } + + @After + public void cleanup() { + Session s = openSession(); + s.getTransaction().begin(); + s.createQuery( "delete from PrimitiveCharacterArrayIdTest$DemoEntity" ).executeUpdate(); + s.getTransaction().commit(); + s.close(); + } + + /** + * Removes two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testMultipleDeletions() { + Session s = openSession(); + s.getTransaction().begin(); + Query query = s.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); + List results = query.list(); + s.delete( results.get( 0 ) ); + s.delete( results.get( 1 ) ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + query = s.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); + assertEquals( 1, query.list().size() ); + s.getTransaction().commit(); + s.close(); + } + + /** + * Updates two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testMultipleUpdates() { + Session s = openSession(); + s.getTransaction().begin(); + Query query = s.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + results.get( 0 ).name = "Different 0"; + results.get( 1 ).name = "Different 1"; + final String lastResultName = results.get( 0 ).name; + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + query = s.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); + results = (List) query.list(); + final Set names = new HashSet( ); + for ( DemoEntity entity : results ) { + names.add( entity.name ); + } + assertTrue( names.contains( "Different 0" ) ); + assertTrue( names.contains( "Different 1" ) ); + assertTrue( names.contains( lastResultName ) ); + s.getTransaction().commit(); + s.close(); + } + + + @Entity + @Table(name="DemoEntity") + public static class DemoEntity { + @Id + public char[] id; + public String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeComparableIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeComparableIdTest.java new file mode 100644 index 0000000000..16ce89e47d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeComparableIdTest.java @@ -0,0 +1,213 @@ +/* + * 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.id.usertype; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Comparator; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.junit.Test; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Configuration; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.type.LongType; +import org.hibernate.usertype.UserType; + +public class UserTypeComparableIdTest extends BaseCoreFunctionalTestCase { + @Override + protected void configure(Configuration configuration) { + configuration.setProperty( AvailableSettings.ORDER_UPDATES, "true" ); + } + + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testUserTypeId() { + Session s = openSession(); + s.beginTransaction(); + SomeEntity e1 = new SomeEntity(); + CustomId e1Id = new CustomId( 1L ); + e1.setCustomId( e1Id ); + SomeEntity e2 = new SomeEntity(); + CustomId e2Id = new CustomId( 2L ); + e2.setCustomId( e2Id ); + s.persist( e1 ); + s.persist( e2 ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + e1 = s.get( SomeEntity.class, e1Id ); + e2 = s.get( SomeEntity.class, e2Id ); + s.delete( e1 ); + s.delete( e2 ); + s.getTransaction().commit(); + s.close(); + } + + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { SomeEntity.class }; + } + + + @TypeDef( + name = "customId", + typeClass = CustomIdType.class + ) + @Entity + @Table(name = "some_entity") + public static class SomeEntity { + + @Id + @Type(type = "customId") + @Column(name = "id") + private CustomId customId; + + public CustomId getCustomId() { + return customId; + } + + public void setCustomId(final CustomId customId) { + this.customId = customId; + } + } + + public static class CustomId implements Serializable, Comparable { + + private final Long value; + + public CustomId(final Long value) { + this.value = value; + } + + public Long getValue() { + return value; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + + CustomId customId = (CustomId) o; + + return !( value != null ? !value.equals( customId.value ) : customId.value != null ); + + } + + @Override + public int hashCode() { + return value != null ? value.hashCode() : 0; + } + + @Override + public int compareTo(CustomId o) { + return value.compareTo( o.value ); + } + } + + public static class CustomIdType implements UserType, Comparator { + + public static final LongType SQL_TYPE = LongType.INSTANCE; + + @Override + public int[] sqlTypes() { + return new int[] { SQL_TYPE.sqlType() }; + } + + @Override + public Object nullSafeGet( + ResultSet resultSet, + String[] names, + SharedSessionContractImplementor sessionImplementor, + Object o) throws HibernateException, SQLException { + Long value = resultSet.getLong( names[0] ); + + return new CustomId( value ); + } + + @Override + public void nullSafeSet( + PreparedStatement preparedStatement, + Object value, + int index, + SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { + CustomId customId = (CustomId) value; + + if ( customId == null ) { + preparedStatement.setNull( index, SQL_TYPE.sqlType() ); + } + else { + preparedStatement.setLong( index, customId.getValue() ); + } + } + + @Override + public Class returnedClass() { + return CustomId.class; + } + + @Override + public int compare(CustomId o1, CustomId o2) { + return o1.getValue().compareTo( o2.getValue() ); + } + + @Override + public boolean equals(Object x, Object y) throws HibernateException { + return x.equals( y ); + } + + @Override + public int hashCode(Object x) throws HibernateException { + return x.hashCode(); + } + + @Override + public Object deepCopy(Object value) throws HibernateException { + return value; + } + + @Override + public boolean isMutable() { + return true; + } + + @Override + public Serializable disassemble(Object value) throws HibernateException { + return (Serializable) value; + } + + @Override + public Object assemble(Serializable cached, Object owner) throws HibernateException { + return cached; + } + + @Override + public Object replace(Object original, Object target, Object owner) throws HibernateException { + return original; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeNonComparableIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeNonComparableIdTest.java new file mode 100644 index 0000000000..d2ecbfca76 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeNonComparableIdTest.java @@ -0,0 +1,199 @@ +/* + * 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.id.usertype; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Comparator; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.junit.Test; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.type.LongType; +import org.hibernate.usertype.UserType; + +public class UserTypeNonComparableIdTest extends BaseCoreFunctionalTestCase { + + @Test + @TestForIssue(jiraKey = "HHH-8999") + @FailureExpected(jiraKey = "HHH-8999") + public void testUserTypeId() { + Session s = openSession(); + s.beginTransaction(); + SomeEntity e1 = new SomeEntity(); + CustomId e1Id = new CustomId( 1L ); + e1.setCustomId( e1Id ); + SomeEntity e2 = new SomeEntity(); + CustomId e2Id = new CustomId( 2L ); + e2.setCustomId( e2Id ); + s.persist( e1 ); + s.persist( e2 ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + e1 = s.get( SomeEntity.class, e1Id ); + e2 = s.get( SomeEntity.class, e2Id ); + s.delete( e1 ); + s.delete( e2 ); + s.getTransaction().commit(); + s.close(); + } + + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { SomeEntity.class }; + } + + + @TypeDef( + name = "customId", + typeClass = CustomIdType.class + ) + @Entity + @Table(name = "some_entity") + public static class SomeEntity { + + @Id + @Type(type = "customId") + @Column(name = "id") + private CustomId customId; + + public CustomId getCustomId() { + return customId; + } + + public void setCustomId(final CustomId customId) { + this.customId = customId; + } + } + + public static class CustomId implements Serializable { + + private final Long value; + + public CustomId(final Long value) { + this.value = value; + } + + public Long getValue() { + return value; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + + CustomId customId = (CustomId) o; + + return !( value != null ? !value.equals( customId.value ) : customId.value != null ); + + } + + @Override + public int hashCode() { + return value != null ? value.hashCode() : 0; + } + } + + public static class CustomIdType implements UserType { + + public static final LongType SQL_TYPE = LongType.INSTANCE; + + @Override + public int[] sqlTypes() { + return new int[] { SQL_TYPE.sqlType() }; + } + + @Override + public Object nullSafeGet( + ResultSet resultSet, + String[] names, + SharedSessionContractImplementor sessionImplementor, + Object o) throws HibernateException, SQLException { + Long value = resultSet.getLong( names[0] ); + + return new CustomId( value ); + } + + @Override + public void nullSafeSet( + PreparedStatement preparedStatement, + Object value, + int index, + SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { + CustomId customId = (CustomId) value; + + if ( customId == null ) { + preparedStatement.setNull( index, SQL_TYPE.sqlType() ); + } + else { + preparedStatement.setLong( index, customId.getValue() ); + } + } + + @Override + public Class returnedClass() { + return CustomId.class; + } + + @Override + public boolean equals(Object x, Object y) throws HibernateException { + return x.equals( y ); + } + + @Override + public int hashCode(Object x) throws HibernateException { + return x.hashCode(); + } + + @Override + public Object deepCopy(Object value) throws HibernateException { + return value; + } + + @Override + public boolean isMutable() { + return true; + } + + @Override + public Serializable disassemble(Object value) throws HibernateException { + return (Serializable) value; + } + + @Override + public Object assemble(Serializable cached, Object owner) throws HibernateException { + return cached; + } + + @Override + public Object replace(Object original, Object target, Object owner) throws HibernateException { + return original; + } + } +}