Spring-boot--集成测试 常见错误解决办法

Spring-boot 集成测试 常见错误解决办法

折腾Spring-boot 把遇到的问题记录下来,方便查找

一、org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: **, could not initialize proxy - no Session

该问题是由于hibernate中事务控制造成的,多出现在JPA设置了@OneToMany的情况下,解决办法有两个:

  1. @OneToMany注解里加fetch = FetchType.EAGER(不推荐,生成环境下浪费查询性能)
  2. 在测试用例中加入@Transactional,表示开启事务。好处在于可以设置fetch = FetchType.LAZY开启懒加载。

猜你喜欢

转载自blog.csdn.net/shrimp_xx/article/details/79413357