JSON序列化程序中的惰性Loadng错误 [英] Lazy Loadng error in JSON serializer
问题描述
我有这样的@OneToOne Hibernate relationShip
public class Address implements Serializable {
私人字符串ID;
私人字符串城市;
私人字符串国家;
// setter getters ommitted
}
public class Student实现Serializable {
private String id;
private String firstName;
private String lastName;
私人地址;
}
地址项目被映射为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);
回报学生;
}
不幸的是,由于Lazy clasees, p>
org.codehaus.jackson.map.JsonMappingException:找不到类org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer的序列化程序,没有(通过引用链:com.vanilla.objects.Student _ $$ _ javassist_1 [address] - > com.vanilla.objects.Address _ $$)创建BeanSerializer的属性(以避免异常,禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) _javassist_0 [handler])
at org.codehaus.jackson.map.ser.StdSerializerProvider $ 1.serialize(StdSerializerProvider.java:62)
我使用OpenSessionInViewInterceptor,它工作得很好。
我明白我可以让用户左键加入HQL查询并以这种方式检索学生和地址并解决问题。我也明白,改变与EAGER的关系将解决它。
但是,我怎样才能使用标准杰克逊消息转换器序列化为JSON懒惰类,这是因为我添加到了我的XML文件中。
最简单的解决方案:不要序列化实体,使用值对象。
如果这不是您的选择,请确保实体Object已分离。
使用JPA(2),您可以使用 EntityManager.detach (实体)
,与普通的Hibernate相当的是 Session.evict(entity)
。
I have such kind of @OneToOne Hibernate relationShip
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;
}
address Item is mapped as LAZY.
Now I want to fetch user and it's address using
session.load(Student.class,id);
In my daoService.
Then I return it as JSON from my 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;
}
Unfortunately, it's not working because of Lazy clasees and I fails with:
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)
I do use OpenSessionInViewInterceptor and it works just fine. I understand that I can user left join HQL query and retrieve student and address that way and solve the problem. I also understand that changing relation to EAGER will solve it.
But how can I serialize to JSON lazy classes using standard jackson message converter which of cause I added to my XML file.
The easiest solution: Don't serialize entities, use Value Objects.
If that is not an option for you, make sure that the entity Object is detached.
With JPA (2), you would use EntityManager.detach(entity)
, with plain Hibernate the equivalent is Session.evict(entity)
.
这篇关于JSON序列化程序中的惰性Loadng错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!