0

私は学校のプロジェクトに取り組んでおり、目標は架空の薬局用の JavaEE アプリケーションを作成することです。

1 つの薬で 1 つまたは複数の病気を治すことができ、1 つの病気は 1 つまたは複数の薬で治すことができるので、これは裏表の関係@OneToManyであると考えました。私もエンティティを持っています。患者は 1 つまたは複数の病気に苦しんでいる可能性があり、1 つの病気が多くの患者を苦しめている可能性があります。クラスを次のようにコーディングしましたが、エンティティからテーブルを生成しようとすると、クラスとクラスの間で互換性のないマッピング例外が発生します。GlassFish サーバーと Eclipse で Derby 接続を使用しています (すべてが適切に構成されているため、間違いなくコードの問題です)。私のクラスは次のとおりです。Drug@ManyToManyDiseasePatientDrugDisease

public class Drug implements Serializable{
@OneToMany(targetEntity = Disease.class, mappedBy = "Cures_For_This_Disease")
private List<Disease> Diseases_Cured_By_This_Drug;
//other fields such as the name, price and origin of the drug
}

public class Disease implements Serializable{
@ManyToMany(targetEntity = Drug.class)
private List<Drug> Cures_For_This_Disease;
@ManyToMany(targetEntity = Patient.class)
private List<Patient> Afflicted_Patients;
//other fields such as name of disease etc.
}

public class Patient implements Serializable{
@OneToMany(targetEntity = Disease.class, mappedBy = "AfflictedPatients")
private List<Disease> Current_Diseases;
//other fields such as Patient name, social sec. nubmer etc
}

私は何を間違っていますか?

4

1 に答える 1

2

クラスとクラスに@ManyToMany注釈が必要です。DrugPatient

public class Drug implements Serializable{
    @ManyToMany
    private List<Disease> Diseases_Cured_By_This_Drug;

}

public class Disease implements Serializable{
    @ManyToMany(mappedBy="Diseases_Cured_By_This_Drug")
    private List<Drug> Cures_For_This_Disease;
}

これにより、2 つの対応するテーブル間に結合テーブルが作成されます。

于 2013-05-19T16:07:43.523 に答える