The many-to-many fields are defined by the tag <many-to-many> :
<entity name="Musicien">
...
<many-to-many name="instruments" ref="Instrument"/>
</entity>
<entity name="Instrument">
...
</entity>
n this case, the object musician has a relationship n p object with an instrument . This relationship is unidirectional.
The entity Musician contains a set I NSTRUMENT :
public class Musicien extends AuditableModel {
...
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Set<Instrument> instruments;
public Set<Instrument> getInstruments() {
return instruments;
}
public void setInstruments(Set<Instrument> instruments) {
this.instruments = instruments;
}
...
}
To create a bidirectional relationship, the target entity also carries a many-to-many relationship to the entity master. This relationship must include a mappedBy attribute to the entity master.
<entity name="Musicien">
...
<many-to-many name="instruments" ref="Instrument"/>
</entity>
<entity name="Instrument">
...
<many-to-many name="musiciens" ref="Musicien" mappedBy="instruments"/>
</entity>