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

查看:208
本文介绍了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?

感谢是一个很大的帮助!

Thank's a lot for your help!

编辑: - 因为它已经解决删除 -

- removed as it has been solved -

编辑2:

看起来像我有我的应用程序栈的一些bug。在模型定义级别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...

但是:这两种方法似乎工作(即createing的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.

有什么区别?有什么关系?

What's the difference? Does it matter?

推荐答案

双向关系,包括拥有和反侧。

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 属性:

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

参见:

  • 2.2.5.3.2. Many-to-many

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

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