通过序列化到json,您可以访问会话上下文之外的未蚀刻数据。
如果使用与链接中完全相同的代码,请尝试将write方法更改为this。
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, HibernateProxy value) throws IOException {
//avoid serializing non initialized proxies
if (value == null || !Hibernate.isInitialized(value)) {
out.nullValue();
return;
}
// Retrieve the original (not proxy) class
Class<?> baseType = Hibernate.getClass(value);
// Get the TypeAdapter of the original class, to delegate the serialization
TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
// Get a filled instance of the original class
Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer()
.getImplementation();
// Serialize the value
delegate.write(out, unproxiedValue);
}
|| !Hibernate.isInitialized(value)
已添加以检查集合是否已初始化,如果未初始化则避免访问它。