UserType to persist Boolean as CHARWe store our Booleans as Y/N in Oracle (common practice in my experience). Below is a user type that will do the conversion for you so you can keep your POJO's as true business objects:
public class CustomBooleanType implements UserType {
public CustomBooleanType() {
}
public int[] sqlTypes() {
return new int[] { Types.CHAR };
}
public Class returnedClass() {
return Boolean.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
return (x == y) || (x != null && y != null && (x.equals(y)));
}
public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o)
throws HibernateException, SQLException {
String val = (String)Hibernate.STRING.nullSafeGet(inResultSet, names[0]);
return BooleanUtil.parseYN(val);
}
public void nullSafeSet(PreparedStatement inPreparedStatement, Object o, int i)
throws HibernateException, SQLException {
String val = BooleanUtil.toYNString((Boolean)o);
inPreparedStatement.setString(i, val);
}
public Object deepCopy(Object o) throws HibernateException {
if (o==null) return null;
return new Boolean(((Boolean)o).booleanValue());
}
public boolean isMutable() {
return false;
}
}
|