我有兩個實體,實體申請人,實體工作應用程序。的關係是訪問新合併記錄時的空指針異常JPA,實體管理器
//Entity Applicants
public class AppPers implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "appPers",fetch = FetchType.EAGER)
private Collection<JobApplications> jobApplicationsCollection;
.........................
}
//Entity JobApplications
public class JobApplications implements Serializable {
@JoinColumn(name = "app_id", referencedColumnName = "APP_ID", insertable = false, updatable = false)
@ManyToOne(optional = false,fetch = FetchType.EAGER)
private AppPers appPers;
所以,以後我做了合併
AppPers ap =em.find(AppPers.class,appId);
ap.setJobApplicationsCollection(c);
em.merge(ap);
em.refresh(ap);
並嘗試執行
//fetch the newly created list
List<JobApplications> list = new ArrayList<JobApplications>();
list = em.createNamedQuery("JobApplications.findAll").getResultList();
//list size is 3 in my case
for(JobApplications ja: list) {
String a = ja.getAppPers().getSurName();
}
姓是AppPers的專欄中,我得到空指針異常。這發生,直到我重新啓動Glassfish然後空指針不會發生。我曾試圖將提取設置爲Eager,但仍然沒有喜悅。我可能會丟失什麼
此外,由於您的關係是可插入的= false,updatable = false,JPA不會正確處理它。這些應該在您爲單個列使用多個綁定時使用,例如多對多的外鍵。 –
thanx爲您的信息,使很多感覺會尋找解決方法。 – user2591526