javax.persistence.EntityNotFoundException: Unable to find报错

  这类错id 可能是10,可能是27,也可能是其他数字

  错误描述:

javax.persistence.EntityNotFoundException: Unable to find 某个类 with id ?

  

原因:

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

解决办法:

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

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

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

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

 3.或者跟业务人员联系,看数据模拟有问题,直接干掉吧

猜你喜欢

转载自www.cnblogs.com/nicknailo/p/9122827.html