hibernate多对一关系中,JSON serializing对象时报JsonMappingException

我在spinrg mvc中用json返回一个pojo,此pojo与另一个pojo存在多对一关系,这时就会报以下exception: 
Java代码   收藏代码
  1. to use the MappingJacksonJsonView to return one of these objects I receive the following exception:  
  2. 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: java.util.HashMap["prospect"]->com.softrek.dms.domain.Prospect["addresses"]->org.hibernate.collection.PersistentSet[0]->com.softrek.dms.domain.Address["prospect"]->com.softrek.dms.domain.Prospect_$$_javassist_1["hibernateLazyInitializer"])  
  3. at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:55)  
  4. at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:218)  
  5. at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:122)  
  6. at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:218)  
  7. at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:122)  


这个错其实很易明了,因为你序列化对象A时,需要把里面的多对一关系的B拿出来,而B里面又有A的集合,如此反复,便报这样的错了,解决这个问题的方法在于在 多对一关系中的的一方的set 集体的get 方法前面添加@JsonIgnore即可,如: 
Java代码   收藏代码
  1. @JsonIgnore public Set getStuedntses() {  
  2.         return studentses;  
  3.     }  

添加这个注解后,无论你是lazy="true" or lazy="false"他都不会去序列化你的了!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值