I'm running into trouble with hibernate. I recently set my hbm2ddl to validate, and it has been complaining a lot about wrong datatypes. I have fixed every problem except for booleans.
I have a field opener
in my class, which is mapped as:
<property column="opener" name="opener" type="boolean"/>
The column opener
is a tinyint (4)
and has a value of 1 or 0. So far I've tried changing the types, but to no avail. I have also tried using the following setting in my hibernate.cfg:
<property name="hibernate.query.substitutions">true 1, false 0</property>
But I am still getting the same error. What am I doing wrong?
org.hibernate.HibernateException: Wrong column type: opener, expected: bit
at org.hibernate.mapping.Table.validateColumns(Table.java:261)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1083)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:317)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
note: I have no access to the database.
If you can't change your SQL type in your table, i recommend you to do this :
<property name="opener" column="opener" type="path.to.your.package.YourClassUserType"/>
and create your class :
import org.hibernate.usertype.UserType;
public class YourClassUserType implements UserType{
...
}
you have to implement methods from the interface UserType. The implementation will transform byte to boolean (because a TINYINT is mapped in byte in Java)
see examples here
good luck :)
For anyone who ran into the same trouble as me, I used a combination of two answers posted here.
I implemented a custom usertype 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);
}
/* boilerplate... */
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 opener field I use type=tinyint_boolean
and it works like a charm :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With