Hello
I've written a custom UserType to deal with a legacy database storing texts as ASCII strings with HTML escapes (instead of, say, UTF8 or some other binary charset.) I'm trying to do the encoding and decoding on the UserType level.
This is the code (standard part collapsed):
Code:
public class HtmlStringType implements UserType
{
public HtmlStringType() {}
private static int[] sqlTypes = new int[] { Types.VARCHAR };
public int[] sqlTypes() { return sqlTypes; }
public Class returnedClass() { return String.class; }
public boolean isMutable() { return false; }
public Object assemble(Serializable cached, Object owner) { return cached; }
public Serializable disassemble(Object value) { return (Serializable) value; }
public Object replace(Object original, Object target, Object owner) { return original; }
public int hashCode(Object x) { return x.hashCode(); }
public boolean equals(Object x, Object y) { return (x == y) || (x != null && x.equals(y)); }
public Object deepCopy(Object o) { return o == null ? null : new String((String) o); }
public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o)
throws SQLException
{
String val = (String) Hibernate.STRING.nullSafeGet(inResultSet, names[0]);
return StringEscapeUtils.unescapeHtml(val);
}
public void nullSafeSet(PreparedStatement inPreparedStatement, Object o, int i)
throws SQLException
{
String val = (String) o;
inPreparedStatement.setString(i, StringEscapeUtils.escapeHtml(val));
}
}
I have applied it to some String properties using @Type. I made sure that the @Type annotation is being recognized, because if I return more than an element in the sqlTypes() array, I get a "property mapping has wrong number of columns" error.
Still my nullSafeGet() method is never entered (I checked with a debugger) and so my custom string encoding / decoding never happens. What am I missing?
Edit: I'm using 3.5.2-Final