2013-10-04 145 views
0

如果我在单个实体A中有多个关系(多对一)朝向实体B(注释为一对多)?我是否必须为B中的每个A出现注释?多重映射Jpa中的一对多

例子:

实体答:

@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; 

难道我已经还插入地区:例如地区

@OneToMany(mappedBy = "birthRegion") 
     private List<Patient> patientsBirthRegion; 

??

+0

是的!!!你必须如果你想有这种关系.. –

+0

非常感谢你! – andPat

回答

2

的下面一对关联映射的,

@ManyToOne 
@Column(name = "birth_region") 
private Region birthRegion; 


@OneToMany(mappedBy = "birthRegion") 
private List<Patient> patientsBirthRegion; 

限定bidirectional association唯一的patient S和它们的birthRegion列表之间。现在,如果您希望在这些区域中的其他regionspatients之间存在类似的关联类型,则需要在这些关联映射之间建立这种类型的关联。