Spring集成JPA配置懒加载两个报错解决办法
Posted LOGO-IT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring集成JPA配置懒加载两个报错解决办法相关的知识,希望对你有一定的参考价值。
<!--配置openmanager--> <filter> <filter-name>openEntity</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openEntity</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在完成上面的配置后会报第二个错误
1 @ManyToOne(fetch = FetchType.LAZY) 2 @JoinColumn(name="department_id") 3 @JsonIgnoreProperties(value = {"hibernateLazyInitializer","handler","fieldHandler"}) 4 private Department department;
1 public class CustomMapper extends ObjectMapper { 2 public CustomMapper() { 3 this.setSerializationInclusion(JsonInclude.Include.NON_NULL); 4 // 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false 5 this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); 6 } 7 }
第二步:配置spring-mvc.xml
1 <!--注解支持--> 2 <mvc:annotation-driven> 3 <mvc:message-converters> 4 <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 5 <property name="supportedMediaTypes"> 6 <list> 7 <value>application/json; charset=UTF-8</value> 8 <value>application/x-www-form-urlencoded; charset=UTF-8</value> 9 </list> 10 </property> 11 <!-- No serializer:配置 objectMapper 为我们自定义扩展后的 CustomMapper,解决了返回对象有关系对象的报错问题 --> 12 <property name="objectMapper"> 13 <bean class="com.logo.aisell.util.CustomMapper"></bean> 14 </property> 15 </bean> 16 </mvc:message-converters> 17 </mvc:annotation-driven>
以上是关于Spring集成JPA配置懒加载两个报错解决办法的主要内容,如果未能解决你的问题,请参考以下文章
Spring Data JPA使用getOne方法报错:Method threw 'org.hibernate.LazyInitializationException' excepti
JPA 懒加载实践 fetch = FetchType.LAZY