雪洗中关村

导航

@Transactional + FetchType.LYZY (hibernate) <---> Exception: could not initialize proxy - no Session;

转自: https://blog.csdn.net/blueheart20/article/details/52912023

 

 4.问题的解决

    尝试1:  在Service方法中新增了@Transactional进行事务添加

    结果1:  无效

   尝试2: 在@OneToMany的方法上,使用@Lazy(false)

    结果2:  无效

    尝试3: 在@OneToMany的参数中使用fetch=FetchType=Eager

    结果3:   问题解决

    尝试4: 在application.properties的配置文件中新增spring.jpa.open-in-view=true

    结果4: 问题解决

 

方法4的解释与说明:

   这个设置选项:

spring.jpa.open-in-view=true

  其实是之前的openEntityManagerInViewInterceptor,解决在Spring MVC与JPA之间Session的声明周期问题

5. 总结

    核心问题在于解决延迟加载为及时加载,及时加载会消耗一定的资源,将其程序的性能,请注意这个问题。

posted on 2018-08-07 16:10  雪洗中关村  阅读(174)  评论(0编辑  收藏  举报