Multiple mapping One to many in Jpa

强颜欢笑 提交于 2019-12-11 18:04:48

问题


If I've, in a single entity A, multiple relations (many to one) towards an entity B (annotated with one to many)? Do I've to put an annotation for each occurrence of A in B?

Example:

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

Entity B: Region for example:

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

Do I've to insert also in Region:

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

??


回答1:


The below pair of association mappings,

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


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

defines the bidirectional association only between a list of patients and their birthRegion. Now if you want similar type of associations between other regions and the patients in those regions, you need to have this type of association mappings among them.



来源:https://stackoverflow.com/questions/19181110/multiple-mapping-one-to-many-in-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!