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
. 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?
我有一个表,它有两个不同的多对多关系到两个不同的表。假设我有用户<--> UserRole <- >角色和用户<- > UserGroups <- >组。由于我是hibernate和数据库映射的新手,我想知道是否让我的用户实体在其中包含属性角色和组,使用@ManytoMany注释都是好的实践,可以接受吗?
i.e.:
例如:
@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>();
1 个解决方案
#1
5
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.
我认为最小化多对多关系是一个好主意,但它是一个自然结构,到处都可以找到。
#1
5
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.
我认为最小化多对多关系是一个好主意,但它是一个自然结构,到处都可以找到。