【发布时间】:2011-06-08 10:37:08
【问题描述】:
我有这种@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 控制器返回:
@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 的关系会解决这个问题。
但是如何使用标准杰克逊消息转换器将其序列化为 JSON 惰性类,这是我添加到我的 XML 文件中的原因。
【问题讨论】:
标签: json hibernate spring spring-mvc lazy-loading