Hibernate JPA —— Unable to find ... with id 0 错误
来源:互联网 发布:手机粉碎软件 编辑:程序博客网 时间:2024/04/28 04:26
之前使用Hibernate JPA 的实体映射时并没有出现此错误,今天再次启动之前的项目时,报此错。此错误特殊之处在于,当使用JPA 获取实体时,其参考的实体若不存在,则判断null 为false ,查看里面属性可见全部为null,但调用该参考实体的任何方法都会导致异常。
错误重现:
假设实体A 中多对一实体B:
@Entity("TABLE_A")class A{@Id@Column("TABLE_A_ID")private int aId;}
实体B 对实体A 则是一对多关系:
@Entity("TABLE_B")class B{@Id@Column("TABLE_B_ID")private int bId;@ManyToOne( fetch = FetchType.LAZY,optional=true)@JoinColumn("A_ID")private A a;}
说明:
optional 为true,则参考表数据必须存在时插入或取数据才会成功。
当用JPA 取出某个B 的对象时,其中aSet 的数据可能不存在,但却有不为空,即会出现所述错误。
解决方法:
在optional 为true 的参考属性上,加 @NotFound(action=NotFoundAction.IGNORE) 注解,当参考数据不存在时,则参考数据为null。所以,最好为:
@Entity("TABLE_B")class B{@Id@Column("TABLE_B_ID")private int bId;@ManyToOne( fetch = FetchType.LAZY,optional=true)@NotFound(action=NotFoundAction.IGNORE)@JoinColumn("A_ID")private A a;}
关于为何出现此错误的原因,不太了解,因为之前不加这个注解,是没有出现此错误的。
阅读全文
0 0
- Hibernate JPA —— Unable to find ... with id 0 错误
- [JPA] javax.persistence.EntityNotFoundException: Unable to find XXXX with id 0 问题原因
- [JPA] javax.persistence.EntityNotFoundException: Unable to find XXXX with id 0 问题原因
- [JPA] javax.persistence.EntityNotFoundException: Unable to find com.duona.base.bean.Business with id
- [JPA错误]javax.persistence.EntityNotFoundException: Unable to find xxx
- [JPA错误]javax.persistence.EntityNotFoundException: Unable to find xxx
- 关于 EntityNotFoundException: Unable to find Person with id 11
- Unable to find models.User with id xx
- python—解决“Unable to find vcvarsall.bat”错误
- python—解决“Unable to find vcvarsall.bat”错误
- python—解决“Unable to find vcvarsall.bat”错误
- Unable to find a team with the given Team ID 问题解决方法
- 真机调试问题:Unable to find a team with the given Team ID
- Android问题解决记录—android.content.res.Resources$NotFoundException: Unable to find resource ID
- easy_install 错误Setup script exited with error: Unable to find vcvarsall.bat解决方法
- 安卓开发——运行错误:Unable to find explicit activity class
- “Unable to find vcvarsall.bat”错误
- 关于“Unable to find vcvarsall.bat”错误
- 4084: [Sdoi2015]双旋转字符串
- Android bind 回调实现Service向activity传递数据
- Android电子白板
- Django 安装
- keil里中断服务函数设置标志位时加volatile
- Hibernate JPA —— Unable to find ... with id 0 错误
- JavaEE成长之路
- nginx 安装及,代理设置
- Kubernetes系统架构简介
- c++显式转换-笔记
- (转载)多线程编程学习三(线程间通信)
- 我眼中SEO的核心宗旨杂烩
- 32位汇编第二讲,编写窗口程序,加载资源,响应消息,以及调用C库函数
- jQuery Mobile学习笔记(四)——列表