0

我有两个实体,实体申请人,实体 JobApplications。关系是

    //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();
    }

surName 是 AppPers 的一列,我得到 NullPointer 异常。这会发生,直到我重新启动 Glassfish,然后空指针不会发生。我尝试将 fetch 设置为 Eager,但仍然没有乐趣。我可能会错过什么

4

1 回答 1

1

我不明白这与 JPA 有何关系。它是普通的旧 Java。你有一个类定义为

public class JobApplications implements Serializable {
    private AppPers appPers;
    ...
}

字段的默认值为null

所以如果你这样做

JobApplications ja = new JobApplications ();
AppPers ap = ja.getAppPers();

ap将为空。所以如果你这样做

JobApplications ja = new JobApplications ();
String a = ja.getAppPers().getSurName();

你会得到一个 NullPointerException。

于 2013-07-20T10:26:43.767 回答