[英]Am I using a ManyToMany mapping in JPA properly?
I am working on a school project, and the goal is to make a JavaEE application for a fictional pharmacy. 我正在开展一个学校项目,目标是为一个虚构的药房制作一个JavaEE应用程序。
Since one drug can cure one or many diseases and one disease can be treated by one or many drugs, I figured it is a @OneToMany
relation on the Drug
side and a @ManyToMany
on the Disease
side. 由于一种药物可以治愈一种或多种疾病,一种疾病可以通过一种或多种药物治疗,我认为它是
Drug
方面的@OneToMany
关系和Disease
方面的@ManyToMany
。 I also have a Patient
entity. 我也有一个
Patient
实体。 A patient may suffer from one or many diseases, and one disease may afflict many patients. 患者可能患有一种或多种疾病,并且一种疾病可能折磨许多患者。 I coded my classes as follows but I get an incompatible-mapping-exception between my
Drug
and Disease
classes when I try to generate tables from entities. 我将我的类编码如下,但是当我尝试从实体生成表时,我在
Drug
和Disease
类之间得到了一个不兼容的映射异常。 I am using a GlassFish server and a Derby connection in Eclipse (everything is well-configured, so it's definitely a code issue). 我在Eclipse中使用GlassFish服务器和Derby连接(一切都配置良好,所以它肯定是代码问题)。 My classes are as follows:
我的课程如下:
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
}
What am I doing wrong? 我究竟做错了什么?
You should have @ManyToMany
annotation in Drug
and Patient
classes. 您应该在
Drug
和Patient
课程中使用@ManyToMany
注释。
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;
}
This should create a join table between the two corresponding tables. 这应该在两个相应的表之间创建一个连接表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.