多次映射Jpa中的一对多

时间:2013-10-04 12:25:30

标签: java jpa one-to-many many-to-one

如果我在单个实体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;

...

1 个答案:

答案 0 :(得分:2)

以下一对关联映射,

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


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

仅在bidirectional association列表及其patient之间定义birthRegion。现在,如果您想在这些区域中的其他regionspatients之间进行类似的关联,则需要在它们之间建立这种类型的关联映射。