1. 程式人生 > 其它 >javax.persistence.EntityNotFoundException: Unable to find報錯

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.或者跟業務人員聯絡,看資料模擬有問題,直接幹掉吧