We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) @JoinTable(name = "user_role", joinColumns = {@JoinColumn(name = "userId",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "roleId",referencedColumnName = "id")}) private List<Role> roles = new ArrayList<>(); @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY) @JoinTable(name = "user_permission", joinColumns = {@JoinColumn(name = "userId",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "permissionId",referencedColumnName = "id")}) private List<Permission> permissions = new ArrayList<>();
@ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY) private List<User> users = new ArrayList<>(); @ManyToMany(cascade = CascadeType.MERGE,fetch = FetchType.LAZY) @JoinTable(name = "role_permission", joinColumns = {@JoinColumn(name = "roleId",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "permissionId",referencedColumnName = "id")}) @CollectionId(columns = @Column(name = "id"), type = @Type(type = "long"), generator = "identity") private List<Permission> permissions = new ArrayList<>();
@ManyToMany(mappedBy = "permissions",fetch = FetchType.LAZY) private List<Role> roles = new ArrayList<>(); @ManyToMany(mappedBy = "permissions",fetch = FetchType.LAZY) private List<User> users = new ArrayList<>();
The text was updated successfully, but these errors were encountered:
No branches or pull requests
我们为什么要区分用户权限的粒度
在不同的应用场景中,我们需要要对用户进行不同粒度的权限控制,如
批量控制权限
我们需要对于权限进行批量的操作。想象这样一种应用场景,你是QQ的不锈钢贵族,你拥有10个不同的权限,同样的不锈钢贵族还有很多。难道QQ对每一个不锈钢贵族的用户都要维护其与10个权限的关联关系么,显然不是这样。这样会造成一定的数据冗余,因为每一个不锈钢贵族与权限的关联关系都是相同的,没有必要维护多次,冗余度会随着不锈钢贵族对应的权限数量的增长而变得更加严重;除此之外,还会对用户的授权和撤权造成一定的麻烦。
因此我们需要引入Role(角色)的概念,Role与Permission建立关联,User再与Role建立关联。
权限粒度设计
综上,有关权限管理,共需要三种Entity
User 和 Role、Permission均是多对多的关系
Role 和 Permission之间是多对多的关系
Permission 的设计
实现
使用Hibernate建立实体和实体之间的关联关系
注意三者之间的多对多的关联关系
The text was updated successfully, but these errors were encountered: