HHH-6533 - Fix and test
This commit is contained in:
parent
37c3266edd
commit
7d593e32e2
|
@ -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<Byte> {
|
||||
public static final ByteTypeDescriptor INSTANCE = new ByteTypeDescriptor();
|
||||
|
@ -36,14 +37,12 @@ public class ByteTypeDescriptor extends AbstractTypeDescriptor<Byte> {
|
|||
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" })
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue