spring boot hibernate 懒加载解决方案

spring boot 是快速构建微服务的新框架。
对于数据访问问题可以直接使用jpa技术,但是在单元测试发现spring jpa存在hibernate懒加载问题。

hibernate懒加载异常

但是spring-boot没有xml配置文件所以现在网络上好多的解决方案并不能适用在spring boot框架中。在遇到该问题苦苦查询后终于无意中发现了解决方案。
Spring application using JPA with Hibernate, lazy-loading issue in unit test
英文不好没有细看,但是可以看到在spring-boot项目中如何通过application设置hibernate的配置选项。
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

spring.jap.properties后面可以接hibernate的各种配置,然后陪孩子会被自动注入的hibernate实例中。
然后再次运行单元测试全部通过,不在出现懒加载异常。

测试通过


猜你喜欢

转载自blog.csdn.net/u013111003/article/details/80927290