2

我在下面映射了 3 个实体类:

@Table(name = "PROVIDER")
public class Provider implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private long id;
    private long npi;
    private String name;
    private String specialization;
    @OneToMany(mappedBy = "provider", cascade = REMOVE)
    @OrderBy
    private List<Treatment> treatments;
    @Transient
    private ITreatmentDAO treatmentDAO;
    // and so forth
}

@Table(name = "PATIENT")
public class Patient implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private long id;
    private long patientid;
    private String name;
    @Temporal(TemporalType.DATE)
    private Date birthDate;
    @OneToMany(cascade = REMOVE, mappedBy = "patient")
    @OrderBy
    private List<Treatment> treatments;
    // and so forth
}

@Table(name = "TREATMENT")
public abstract class Treatment implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private long id;
    private String diagnosis;
    @ManyToOne
    @JoinColumn(name = "patient_fk", referencedColumnName = "id")
    private Patient patient;
    @ManyToOne
    @JoinColumn(name = "provider_fk", referencedColumnName = "id")
    private Provider provider;
    // and so forth
}

当我在 Patient 或 Provider 上调用 getTreatments 时,我希望 JPA 填写治疗属性。但是,这只适用于提供者。无论我尝试什么,patient.getTreatments() 总是返回空。

我错过了什么?

这是服务器端的单元测试:

@Test
public void TestAddTreatment() throws PatientExn, ProviderExn {
        GregorianCalendar bd = new GregorianCalendar();
        bd.set(1977, 03, 13);
    Patient patient = this.pf.createPatient(900L, "Orezi.Dauda", bd.getTime());
    Provider provider = this.prf.createProvider(6777L, "Sam.OKafor");
    this.pdao.getEntityManager().getTransaction().begin();
    this.pdao.addPatient(patient);
    this.pdao.getEntityManager().getTransaction().commit();
    this.prdao.getEntityManager().getTransaction().begin();
    this.prdao.addProvider(provider);
    provider.addDrugTreatment(900L, "Malaria", "Chloroquine", (float)5.0);
    this.prdao.getEntityManager().getTransaction().commit();
    List<Long> providerTids = this.prdao.getProviderByNpi(6777L).getTreatmentIds();
    List<Long>patientTids = this.pdao.getPatientByPatientId(900L).getTreatmentIds();
    assert(providerTids.size() == 1); //Passes
    assert(patientTids.size() == 1);// Fails
}
4

2 回答 2

0

将行替换@OneToMany(cascade = REMOVE, mappedBy = "patient")

@OneToMany(cascade = CascadeType.ALL, mappedBy = "patient", orphanRemoval = true) and try it should work
于 2013-04-16T05:50:01.120 回答
0

确保在创建/更新对象时添加到关系的双方。

于 2013-04-16T13:34:25.033 回答