同一实体对象之间多对多关系的JPA注解 [英] JPA Annotations for many-to-many relation between objects of the same entity

查看:28
本文介绍了同一实体对象之间多对多关系的JPA注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现角色层次结构,但对 JPA 注释比较陌生.

I want to implement a Role Hierarchy but am rather new to JPA Annotations.

我有一个带有名称和 ID 的角色实体(通过 AbstractPersistable 隐式):

I have a Role Entity with a name and an id(implicit via AbstractPersistable):

@Entity
@Table(name="role")
public class Role extends AbstractPersistable<Long> {
    private static final long serialVersionUID = 8127092070228048914L;  
    private String name;

现在我希望能够定义以下关系:

Now I want to be able to define the following relationships:

  • 一个角色可以有多个子角色
  • 一个角色可以是多个角色的子角色

我将如何使用 Hibernate 注释来做到这一点?我可以在角色实体中定义这个吗

How would I do that with Hibernate annotations? Can I define this inside the Role Entity

@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable( name = "role_hierarchy", 
            joinColumns = { @JoinColumn(name = "role_id")}, 
            inverseJoinColumns={@JoinColumn(name="child_role_id")})  
private List<Role> roles;

@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable( name = "role_hierarchy", 
            joinColumns = { @JoinColumn(name = "child_role_id")}, 
            inverseJoinColumns={@JoinColumn(name="role_id")})  
private List<Role> children;

我在正确的轨道上吗?我错过了什么?

Am I on the right track? What am I missing?

非常感谢您的帮助!

- 已解决 -

编辑 2:

看起来我的应用程序堆栈中有一些错误.在模型定义级别,role_hierarchy 工作得很好,所以不要介意编辑 1...

Looks like I have some bug in my application stack. On the model defining level the role_hierarchy is working out just fine, so never mind EDIT 1...

但是:两种方式似乎都有效(即创建 m:n 表条目,级联删除和检索实体的父级和子级):

BUT: Both ways seem to work (that is createing the m:n table entry, cascading delete and retrieval of parents and children for an entity):

  • 我的提议是在 @ManyToMany 注释处为双方定义一个没有 mappedBy 属性的连接列
  • 以及定义拥有方和反向方.
  • my proposal of defining a join column for both sides with no mappedBy property at the @ManyToMany annotation
  • as well as defining an owning side and an inverse side.

有什么区别?重要吗?

推荐答案

双向关系由拥有和反向组成.

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;

在反面,您使用 mappedBy 属性指向相应的拥有方:

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).

另见:

这篇关于同一实体对象之间多对多关系的JPA注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆