HHH-6533 ByteTypeDescriptor is not working properly

This commit is contained in:
Strong Liu 2011-09-07 16:59:48 +08:00
parent 0418725042
commit 6032c4dbfb
3 changed files with 162 additions and 2 deletions

View File

@ -29,6 +29,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();
@ -39,12 +40,12 @@ public class ByteTypeDescriptor extends AbstractTypeDescriptor<Byte> {
@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" })

View File

@ -0,0 +1,104 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.typedescriptor;
import junit.framework.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit.functional.FunctionalTestCase;
import org.hibernate.testing.junit.functional.FunctionalTestClassTestSuite;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class ByteTest extends FunctionalTestCase {
public static final byte TEST_VALUE = 65;
public ByteTest(String str) {
super( str );
}
public static Test suite() {
return new FunctionalTestClassTestSuite( ByteTest.class );
}
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class[] {
VariousTypesEntity.class
};
}
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();
assertNotNull( entity );
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();
assertNotNull( entity );
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();
assertNotNull( entity );
assertEquals( Byte.MAX_VALUE, entity.getByteData() );
transaction.commit();
session.close();
}
}

View File

@ -0,0 +1,55 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.typedescriptor;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @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;
}
}