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實例中。
然后再次運行單元測試全部通過,不在出現懶加載異常。
測試通過