JPA Annotations for many-to-many relation between objects of the same entity

只愿长相守 提交于 2019-12-03 08:25:25

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<Role> roles;

At the inverse side you point at the corresponding owning side with mappedBy attribute:

@ManyToMany(cascade = CascadeType.MERGE, mappedBy = "roles")
private List<Role> 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:

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