@ResponseBody 与Hibernate懒加载LAZY失效的问题

xiaoxiao2021-02-28  26

@ResponseBody将Object转为JSON 如果Object的关联属性用了LAZY加载; LAZY将会失效,将关联数据也查出。这个时候需要给转换MappingJackson2HttpMessageConverter  的 ObjectMapper注册Hibernate4Module。

第一步:  需要下载 jackson-datatype-hibernate4-2.23.jar 这个包(注意版本对应)

第二步:创建一个类继承ObjectMapper(com.fasterxml.jackson.databind包下的);

import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module; public class HibernateAwareObjectMapper extends ObjectMapper { public HibernateAwareObjectMapper() { registerModule(new Hibernate4Module()); } }

第三步: 修改在springmvc的配置文件applicationContext.xml中节点。

<mvc:annotation-driven> <mvc:message-converters> <!-- Use the HibernateAware mapper instead of the default --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.boleprint.util.HibernateAwareObjectMapper" /> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>

参考网页:

http://blog.csdn.net/derek_yule/article/details/53114530

http://blog.csdn.net/liming8291/article/details/42554179

http://blog.csdn.net/sqh201030412/article/details/55511636

http://blog.csdn.net/alan_liuyue/article/details/53433325

http://blog.csdn.net/ms143014/article/details/51436939

转载请注明原文地址: https://www.6miu.com/read-1950077.html

最新回复(0)