Skip to content

Bidirectional mapping

Kais OMRI edited this page Aug 17, 2018 · 2 revisions

RelMongo bidirectional mapping


Since the 2.2.0 version, RelMongo introduces the mappedBy property to enable bidirectional associations.

For example, when you have a Person document which references its cars documents, you can link a car to its owner and fetch it from the Car class. RelMongo will do the magic for you without changing the car document.

To use the OneToOne bidirectional mapping follow the following example:

// in person class
    @OneToOne(fetch=FetchType.EAGER)
    @JoinProperty(name = "passport")
    private Passport passport;

//in passport class 
   @OneToOne(mappedBy = "passport", fetch = FetchType.EAGER)
    private Person owner;

To use the OneToMany bidirectinal mapping follow the following example:

//in person class
    @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinProperty(name="carsrefs")
    private List<Car> cars;

//in car class
    @ManyToOne(mappedBy="cars")
    private Person owner;

Clone this wiki locally