Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-8999 : Byte array as ID, in composite ID, or as version causes Nu… #1294

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.internal.util.compare;

import java.lang.reflect.Array;
import java.util.Comparator;

/**
* @author Gail Badner
*/
public final class ArrayComparator<X,Y extends Comparable> implements Comparator<X> {
public static final Comparator<byte[]> PRIMITIVE_BYTE_ARRAY_COMPARATOR = new ArrayComparator<byte[],Byte>( Byte.class );
public static final Comparator<Byte[]> BYTE_ARRAY_COMPARATOR = new ArrayComparator<Byte[],Byte>( Byte.class );
public static final Comparator<char[]> PRIMITIVE_CHARACTER_ARRAY_COMPARATOR = new ArrayComparator<char[],Character>( Character.class );
public static final Comparator<Character[]> CHARACTER_ARRAY_COMPARATOR = new ArrayComparator<Character[],Character>( Character.class );

private final Class<Y> elementClass;

private ArrayComparator(Class<Y> elementClass) {
this.elementClass = elementClass;
}

@Override
@SuppressWarnings({ "unchecked" })
public int compare(X o1, X o2) {
final int lengthToCheck = Math.min( Array.getLength( o1 ), Array.getLength( o2 ) );
for ( int i = 0 ; i < lengthToCheck ; i++ ) {
int comparison = ComparableComparator.INSTANCE.compare(
elementClass.cast( Array.get( o1, i ) ),
elementClass.cast( Array.get( o2, i ) )
);
if ( comparison != 0 ) {
return comparison;
}
}
return Array.getLength( o1 ) - Array.getLength( o2 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Comparator;

import org.hibernate.HibernateException;
import org.hibernate.engine.jdbc.BinaryStream;
import org.hibernate.engine.jdbc.internal.BinaryStreamImpl;
import org.hibernate.internal.util.compare.ArrayComparator;
import org.hibernate.type.descriptor.WrapperOptions;

/**
Expand Down Expand Up @@ -71,6 +73,11 @@ public Byte[] fromString(String string) {
return bytes;
}

@Override
public Comparator<Byte[]> getComparator() {
return ArrayComparator.BYTE_ARRAY_COMPARATOR;
}

@SuppressWarnings({ "unchecked" })
@Override
public <X> X unwrap(Byte[] value, Class<X> type, WrapperOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import java.io.StringReader;
import java.sql.Clob;
import java.util.Arrays;
import java.util.Comparator;

import org.hibernate.engine.jdbc.CharacterStream;
import org.hibernate.engine.jdbc.internal.CharacterStreamImpl;
import org.hibernate.internal.util.compare.ArrayComparator;
import org.hibernate.type.descriptor.WrapperOptions;

/**
Expand Down Expand Up @@ -51,6 +53,11 @@ public int extractHashCode(Character[] chars) {
return hashCode;
}

@Override
public Comparator<Character[]> getComparator() {
return ArrayComparator.CHARACTER_ARRAY_COMPARATOR;
}

@SuppressWarnings({ "unchecked" })
@Override
public <X> X unwrap(Character[] value, Class<X> type, WrapperOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Comparator;

import org.hibernate.HibernateException;
import org.hibernate.engine.jdbc.BinaryStream;
import org.hibernate.engine.jdbc.internal.BinaryStreamImpl;
import org.hibernate.internal.util.compare.ArrayComparator;
import org.hibernate.type.descriptor.WrapperOptions;

/**
Expand Down Expand Up @@ -72,6 +74,11 @@ public byte[] fromString(String string) {
return bytes;
}

@Override
public Comparator<byte[]> getComparator() {
return ArrayComparator.PRIMITIVE_BYTE_ARRAY_COMPARATOR;
}

@SuppressWarnings({ "unchecked" })
public <X> X unwrap(byte[] value, Class<X> type, WrapperOptions options) {
if ( value == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import java.io.StringReader;
import java.sql.Clob;
import java.util.Arrays;
import java.util.Comparator;

import org.hibernate.engine.jdbc.CharacterStream;
import org.hibernate.engine.jdbc.internal.CharacterStreamImpl;
import org.hibernate.internal.util.compare.ArrayComparator;
import org.hibernate.type.descriptor.WrapperOptions;

/**
Expand Down Expand Up @@ -51,6 +53,11 @@ public int extractHashCode(char[] chars) {
return hashCode;
}

@Override
public Comparator<char[]> getComparator() {
return ArrayComparator.PRIMITIVE_CHARACTER_ARRAY_COMPARATOR;
}

@SuppressWarnings({ "unchecked" })
public <X> X unwrap(char[] value, Class<X> type, WrapperOptions options) {
if ( value == null ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.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
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
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.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
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
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;
}
}