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
}

患者またはプロバイダーのいずれかで 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 に答える