一、问题出现背景:
- @OneToMany里面用主键查询findById()报空指针,点进去看最后实现类SimpleJpaRepository.class发现其中有一个Set<>集合报错,原因未知:Unable to evaluate the expression Method threw 'java.lang.NullPointerException' exception.
二、可能原因及尝试过程:
1、Hibernate 懒加载 exception及原文
- 在我们的系统中,session是被立即关闭的,也就是在读取了name,password,phone等基本属性后,session 已经 close了,再进行 lazy loaiding 就会有异常
2、懒加载异常错误
-
问题大概意思就是,hibernate的懒加载出现异常,由于seesion被释放了。自己调试了发现是在找下一级关系的时候,无法找到目标实体类导致的。网上找过一些方法都是让你把hibernate实体映射的由
fetch=FetchType.LAZY
改为这种FetchType.EAGER
但是也是无补于事。直到看到这篇博客他给了一个解决方案在service层的方法添加 @Transactional开启事务,最后完美解决了,下一级的关系也找到了 -
但是我的findById是没有事务的,以前别的@OneToOne查询是没有问题的,而都加了事务了,而且@OneToMany(fetch = FetchType.EAGER,所以可能是其它问题
3、
- Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate com.XXX.model.SecuritySystem.toString()
- 使用了注解import javax.validation.constraints.NotNull;而不是import com.sun.istack.internal.NotNull;造成@Data注解生成的构造方法是全属性构造方法,而没有无参构造函数,造成@Mapper映射报错
-
org.springframework.orm.jpa.JpaSystemException: Found two representations of same collection: com.shuijingnet.zhongzhen.tongchou.model.SecuritySystem.securitySystemPictureSet; nested exception is org.hibernate.HibernateException: Found two representations of same collection: com.shuijingnet.zhongzhen.tongchou.model.SecuritySystem.securitySystemPictureSet
三、最终解决:
备注: 本文主要是借助博客环境,同大家讨论异常问题解决的办法,欢迎大家评论,谢谢! |