首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JPA联系关系找不到关联信息

2013-07-09 
JPA关联找不到关联信息javax.persistence.EntityNotFoundException: Unable to find XXXX with id 17290??

JPA关联找不到关联信息
javax.persistence.EntityNotFoundException: Unable to find XXXX with id 17290

??发现JPA在关联的时候,找不到子表中的数据,后来发现时有其他同事把子表中的数据删除了,造成该错误。

?

由于表结构中没有创建关联关系,所以一不小心就容易出现这个错误。

下面就记录下我的解决方法:

1. 将对应的数据补上,避免关联的时候一端出现空数据,从而造成错误。

? ? 这个方法对于数据比较多的时候,还是比较纠结的,但是能保证数据的完整性

2. 为关联的字段设置注解,没有找到对象时,给出相应的处理。

? ? 为字段加上@NotFound?注解,该注解的target是@Target( { METHOD, FIELD }),所以可以在方法和字段上进行添加,该注解默认返回的数据为:

NotFoundAction action() default NotFoundAction.EXCEPTION

?即需要我们处理对应的异常信息,还有一种方式IGNORE:

?

?

public enum NotFoundAction {/** * raise an exception when an element is not found (default and recommended) */EXCEPTION,/** * ignore the element when not found in DB */IGNORE}

?通过添加ignore的属性,那么在找不到子表数据时,直接返回null。

?

?

?

热点排行