2013-10-04 209 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之間存在類似的關聯類型,則需要在這些關聯映射之間建立這種類型的關聯。