parent
10fd4635be
commit
52733a2869
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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 <p.krauzowicz@visiona.pl>
|
||||
* @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<DemoEntity> results = (List<DemoEntity>) 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<DemoEntity>) query.list();
|
||||
final Set<String> names = new HashSet<String>( );
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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 <p.krauzowicz@visiona.pl>
|
||||
* @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<DemoEntity> results = (List<DemoEntity>) 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<DemoEntity>) query.list();
|
||||
final Set<String> names = new HashSet<String>( );
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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 <p.krauzowicz@visiona.pl>
|
||||
* @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<DemoEntity> results = (List<DemoEntity>) 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<DemoEntity>) query.list();
|
||||
final Set<String> names = new HashSet<String>( );
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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 <p.krauzowicz@visiona.pl>
|
||||
* @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<DemoEntity> results = (List<DemoEntity>) 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<DemoEntity>) query.list();
|
||||
final Set<String> names = new HashSet<String>( );
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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<CustomId> {
|
||||
|
||||
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<CustomId> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue