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

我在spinrg mvc中用json返回一个pojo,此pojo与另一个pojo存在多对一关系,这时就会报以下exception:
to use the MappingJacksonJsonView to return one of these objects I receive the following exception:
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"])
at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:55)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:218)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:122)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:218)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:122)


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

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

你可能感兴趣的:(java,json,mvc,Hibernate)