Domanda

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;

??

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top