我有3个班级 - 患者,过敏清单和过敏症。 Patient和Allergy都是@Entity,AllergyList只有一个带有Allergiest列表的变量。
@Entity
public class Allergy {
private String id;
private String name;
public Allergy(String i, String n) {
id=i;
name=n;
}
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String n) {
this.name = n;
}
}
过敏类必须有一个id,并且在我的程序中被其他类引用。
public class AllergyList {
private List<Allergy> allergies = new ArrayList<Allergy>();
public List<Allergy> getAllergies() {
return allergies;
}
public void setAllergies(List<Allergy> a) {
this.allergies = a;
}
}
我需要拥有这个类,因为我继承了该程序,并且无法更改现有的代码库。
@Entity
public class Patient {
private String id;
private AllergyList allergyList = new AllergyList();
public Patient() {}
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public AllergyList getAllergyList() {
return allergyList;
}
public void setAllergyList(AllergyList allergyList) {
this.allergyList = allergyList;
}
}
主要......
public static void main(String[] args) {
Patient patient = new Patient();
patient.setId("100001");
List<Allergy> allergies = new ArrayList<Allergy>();
allergies.add(new Allergy("1","Dust"));
allergies.add(new Allergy("3","Apple"));
allergies.add(new Allergy("4","Bee"));
patient.allergyList.setAllergies(allergies);
.
.
session.save(patient);
.
.
}
所以上面三个类是我给出的。我需要创建包含以下内容的allergy_list表:
allergy_list
patient_id
allergy_id
我知道我可以在Patient类中创建一个List,但编写程序的方式我需要通过AllergyList类访问Allergy。我该如何注释才能实现这一目标?它甚至可能吗?