I am writing the following custom serializable UserType:
public class SerUserType extends MutableUserType {
protected Class klass;
protected SerA ser=F.g(SerA.class);
public Class returnedClass() {
return klass;
}
public int[] sqlTypes() {
return new int[] {Types.BLOB};
}
public boolean equals(Object x, Object y) throws HibernateException {
return ObjectUtils.equals(x, y);
}
public Object deepCopy(Object value) {
klass=value.getClass();
Copyable copyable=(Copyable)value;
Object copy=copyable.copy();
return copy;
}
public Object nullSafeGet(ResultSet resultSet,String[] names,SessionImplementor session,Object owner)
throws HibernateException,SQLException {
byte[] b=(byte[])BlobType.INSTANCE.nullSafeGet(resultSet,names,session,owner);
return ser.deser(b,klass);
}
public void nullSafeSet(PreparedStatement preparedStatement,Object value,int index,SessionImplementor session)
throws HibernateException,SQLException {
BlobType.INSTANCE.nullSafeSet(preparedStatement,ser.ser(value),index,session);
}
}
I might even override the registered serialable class with it. In case you're interested, the implementation of SerA is the protostuff serializer.
Anyway, it doesn't appear that any of the methods providing the object in this interface are called before nullSafeGet so it is impossible to determine the class of the object we're working with, making it impossible to obtain the class for the serialization call.
Therefore, it appears that the only solution is to make a ParameterizedType and pass the object's class as a property. :(