diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteTypeDescriptor.java index 9ada243976..c86d9cfdb3 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteTypeDescriptor.java @@ -28,6 +28,7 @@ import org.hibernate.type.descriptor.WrapperOptions; * Descriptor for {@link Byte} handling. * * @author Steve Ebersole + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ public class ByteTypeDescriptor extends AbstractTypeDescriptor { public static final ByteTypeDescriptor INSTANCE = new ByteTypeDescriptor(); @@ -36,14 +37,12 @@ public class ByteTypeDescriptor extends AbstractTypeDescriptor { super( Byte.class ); } - @SuppressWarnings({ "UnnecessaryUnboxing" }) public String toString(Byte value) { - return Integer.toHexString( value.byteValue() - Byte.MIN_VALUE ); + return value == null ? null : value.toString(); } - @SuppressWarnings({ "UnnecessaryBoxing" }) public Byte fromString(String string) { - return Byte.valueOf( (byte) (Integer.parseInt( string, 16) + Byte.MIN_VALUE) ); + return Byte.valueOf( string ); } @SuppressWarnings({ "unchecked" }) diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/typedescriptor/ByteTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/typedescriptor/ByteTest.java new file mode 100644 index 0000000000..5fcdf29829 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/typedescriptor/ByteTest.java @@ -0,0 +1,76 @@ +package org.hibernate.test.typedescriptor; + +import org.junit.Assert; +import org.junit.Test; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class ByteTest extends BaseCoreFunctionalTestCase { + public static final byte TEST_VALUE = 65; + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + VariousTypesEntity.class + }; + } + + @Test + @TestForIssue( jiraKey = "HHH-6533" ) + public void testByteDataPersistenceAndRetrieval() { + Session session = openSession(); + Transaction transaction = session.beginTransaction(); + VariousTypesEntity entity = new VariousTypesEntity(); + entity.setId( 1 ); + entity.setByteData( TEST_VALUE ); + session.persist( entity ); + transaction.commit(); + session.close(); + + // Testing sample value. + session = openSession(); + transaction = session.beginTransaction(); + entity = (VariousTypesEntity) session.createQuery( + " from VariousTypesEntity " + + " where byteData = org.hibernate.test.typedescriptor.ByteTest.TEST_VALUE " + ).uniqueResult(); + Assert.assertNotNull( entity ); + Assert.assertEquals( TEST_VALUE, entity.getByteData() ); + entity.setByteData( Byte.MIN_VALUE ); + session.update( entity ); + transaction.commit(); + session.close(); + + // Testing minimal value. + session = openSession(); + transaction = session.beginTransaction(); + entity = (VariousTypesEntity) session.createQuery( + " from VariousTypesEntity " + + " where byteData = java.lang.Byte.MIN_VALUE " + ).uniqueResult(); + Assert.assertNotNull( entity ); + Assert.assertEquals( Byte.MIN_VALUE, entity.getByteData() ); + entity.setByteData( Byte.MAX_VALUE ); + session.update( entity ); + transaction.commit(); + session.close(); + + // Testing maximal value. + session = openSession(); + transaction = session.beginTransaction(); + entity = (VariousTypesEntity) session.createQuery( + " from VariousTypesEntity " + + " where byteData = java.lang.Byte.MAX_VALUE " + ).uniqueResult(); + Assert.assertNotNull( entity ); + Assert.assertEquals( Byte.MAX_VALUE, entity.getByteData() ); + transaction.commit(); + session.close(); + } +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/typedescriptor/VariousTypesEntity.java b/hibernate-core/src/matrix/java/org/hibernate/test/typedescriptor/VariousTypesEntity.java new file mode 100644 index 0000000000..7a8e9d82cc --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/typedescriptor/VariousTypesEntity.java @@ -0,0 +1,33 @@ +package org.hibernate.test.typedescriptor; + +import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Lob; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +public class VariousTypesEntity implements Serializable { + @Id + private Integer id; + + private byte byteData; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public byte getByteData() { + return byteData; + } + + public void setByteData(byte byteData) { + this.byteData = byteData; + } +}