For those who faced the same problems as me, I used a combination of the two answers posted here.
I applied a custom type to handle my tinyint field:
public class TinyIntegerToBoolean implements UserType { public int[] sqlTypes() { return new int[]{Types.TINYINT}; } public Class returnedClass() { return Boolean.class; } public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor si, Object owner) throws HibernateException, SQLException { return (rs.getByte(names[0]) != 0); } public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor si) throws HibernateException, SQLException { st.setByte(index, Boolean.TRUE.equals(value) ? (byte) 1 : (byte) 0); } public boolean isMutable() { return false; } public boolean equals(Object x, Object y) throws HibernateException { if (x == null || y == null) { return false; } else { return x.equals(y); } } public int hashCode(Object x) throws HibernateException { assert (x != null); return x.hashCode(); } public Object deepCopy(Object value) throws HibernateException { return value; } public Object replace(Object original, Object target, Object owner) throws HibernateException { return original; } public Serializable disassemble(Object value) throws HibernateException { return (Serializable) value; } public Object assemble(Serializable cached, Object owner) throws HibernateException { return cached; } }
Then I added the following to my mappings:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <typedef class="com.test.model.TinyIntegerToBoolean" name="tinyint_boolean"/> </hibernate-mapping>
Then in my opening field I use type=tinyint_boolean
and it works like a charm :)
Terraego
source share