I want to implement a Role Hierarchy but am rather new to JPA Annotations.
I have a Role Entity with a name and an id(implicit via AbstractPersistable
):
Bidirectional relationship consists of owning and inverse sides.
At the owning side you declare physical properties of the relationship:
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "role_hierarchy",
joinColumns = { @JoinColumn(name = "role_id")},
inverseJoinColumns={@JoinColumn(name="child_role_id")})
private List roles;
At the inverse side you point at the corresponding owning side with mappedBy
attribute:
@ManyToMany(cascade = CascadeType.MERGE, mappedBy = "roles")
private List children;
For many-to-many relationships it doesn't matter which side is the owning side (as long as you modify both sides consistently, since only changes at the owning side are propagated to the database).
See also: