我正在编写以下自定义可序列化 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);
}
}
我什至可以用它覆盖已注册的可序列化类。如果您有兴趣,SerA 的实现是 protostuff 序列化程序。
无论如何,在此接口中提供对象的任何方法似乎都没有在 nullSafeGet 之前调用,因此无法确定我们正在使用的对象的类,从而无法获取用于序列化调用的类.
因此,似乎唯一的解决方案是制作一个 ParameterizedType 并将对象的类作为属性传递。:(