[英]Multiple mapping One to many in Jpa
如果我在单个实体A中与一个实体B有多个关系(多对一)(注释为一对多)? 我是否必须为B中每次出现的A都添加注释?
例:
实体A:
@Entity
@Table(name = "patient")
@TableGenerator(name = "tab_gen_pa", initialValue = 30000, allocationSize = 1)
public class Patient implements Serializable, Comparable<Patient> {
@ManyToOne
@Column(name = "birth_region")
private Region birthRegion;
@ManyToOne
@Column(name = "birth_province", length = 2)
private Province birthProvince;
@ManyToOne
@Column(name = "birth_municipality")
private Municipality birthMunicipality;
@Column(name = "living_region")
@ManyToOne
private Region livingRegion;
@Column(name = "living_province", length = 2)
@ManyToOne
private Province livingProvince;
@Column(name = "living_municipality")
@ManyToOne
private Municipality livingMunicipality;
实体B:区域,例如:
@Entity
@Table(name = "region")
@TableGenerator(name = "tab_gen_re", initialValue = 30, allocationSize = 1)
public class Region implements Serializable {
@OneToMany(mappedBy = "livingRegion")
private List<Patient> patients;
我是否还要在Region中插入:
@OneToMany(mappedBy = "birthRegion")
private List<Patient> patientsBirthRegion;
??
下面的一对关联映射,
@ManyToOne
@Column(name = "birth_region")
private Region birthRegion;
@OneToMany(mappedBy = "birthRegion")
private List<Patient> patientsBirthRegion;
定义仅在patient
列表与他们的birthRegion
之间的bidirectional association
。 现在,如果您希望其他regions
与这些区域的patients
之间具有相似的关联类型,则需要在它们之间具有这种关联映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.