JPA]javax.persistence.EntityNotFoundException: Unable to find XX类 with id xx问题

 exception:javax.persistence.EntityNotFoundException: Unable to find com.travelsky.b2g.bean.TBasUploadFile with id 201801232236521091

原因:

    无论是@OneToOne 还是@ManyToOne,出现这个原因都是因为子表(被关联表)中没有主表(关联表)中ID所对应的记录。

 

解决办法:

    1. 检查为什么子表中没有主表中ID对应的记录

    2. 如果子表中没有主表ID对应的记录也可以正常加载数据,那么需要在主表字段上加一个@NotFound Annotation。示例如下:

    @OneToOne(optional=true )
    @JoinColumn(name="companyId",insertable=false, updatable=false)
    @NotFound(action=NotFoundAction.IGNORE)
    private Company company;

    这样,当子表中没找到数据时,主表中对应的field就是null,而不会报错了。

猜你喜欢

转载自xinjiatao.iteye.com/blog/2422221
xx