我有这种@OneToOne Hibernate 关系
public class Address implements Serializable {
private String id;
private String city;
private String country;
//setter getters ommitted
}
public class Student implements Serializable {
private String id;
private String firstName;
private String lastName;
private Address address;
}
地址项映射为 LAZY。
现在我想获取用户及其地址使用
session.load(Student.class,id);
在我的 daoService 中。
然后我将它作为 JSON 从我的 Spring MVC Controller 返回:
@RequestMapping(value="/getStudent.do",method=RequestMethod.POST)
@ResponseBody
public Student getStudent(@RequestParam("studentId") String id){
Student student = daoService.getStudent(id);
return student;
}
不幸的是,由于 Lazy clasees,它不起作用,我失败了:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.vanilla.objects.Student_$$_javassist_1["address"]->com.vanilla.objects.Address_$$_javassist_0["handler"])
at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:62)
我确实使用 OpenSessionInViewInterceptor 并且它工作得很好。
我知道我可以用户离开加入 HQL 查询并检索学生并通过这种方式解决问题。我也明白改变与 EAGER 的关系将解决它。
但是我如何使用标准的 jackson 消息转换器序列化到 JSON 惰性类,这是我添加到我的 XML 文件中的原因。
最佳答案
最简单的解决方案:不要序列化实体,使用值对象。
如果这不是您的选择,请确保实体 Object 已分离。
使用 JPA (2),您将使用 EntityManager.detach(entity)
, 使用普通的 Hibernate 等效于 Session.evict(entity)
.
关于json - JSON 序列化程序中的延迟加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6277439/