简体   繁体   English

一个实体可以有多个多对多关系吗?

[英]Can an entity have more than one many-to-many relations?

I have a table that has two different many-to-many relations to two different tables. 我有一个表与两个不同的表有两个不同的多对多关系。 Let's say I have User <---> UserRole <--> Role and User <--> UserGroups <--> Groups . 假设我有User <---> UserRole < - > RoleUser < - > UserGroups < - > Groups Since I am new to hibernate and database mapping I was wondering if having my User entity have attributes roles and groups in it, both with @ManytoMany annotations is good practice and acceptable? 由于我是hibernate和数据库映射的新手,我想知道如果我的User实体中有属性角色和组,那么@ManytoMany注释都是好的做法并且可以接受吗?

ie: 即:

 @Entity(name = "User")
 @Table(name = "USER")
 public class User {

 .... /* Obviously Id would go here and all other attributes */

 @ManyToMany(cascade = CascadeType.ALL)
 @JoinTable(name = "UserRole", joinColumns = { @JoinColumn(name="USER_ID") },
            inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") } )
 private Set<Role> roles = new HashSet<Role>();

 @ManyToMany(cascade = CascadeType.ALL)
 @JoinTable(name = "UserGroup", joinColumns = { @JoinColumn(name="USER_ID") },
            inverseJoinColumns = { @JoinColumn(name = "GROUP_ID") } )
 private Set<Group> groups = new HashSet<Group>();

Sure; 当然; lots of types have multiple many-to-many. 许多类型都有多个多对多。

I think minimizing many-to-many relationships is a good idea, but it's a natural structure, and is found all over the place. 我认为尽量减少多对多关系是一个好主意,但它是一种自然的结构,并且遍布各处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM