有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Hibernate(注释)创建具有权限的简单用户/组模型

这很可能是一个非常基本的问题,但是:

基本上,用户实体有一个Id和一个特权枚举。 该组有一个id和一个名称

我想知道如何建立一个关系模型,其中一个用户可以在多个组中,在不同的组中拥有不同的权限级别。当然,每个小组都可以有多个成员

我用Hibernate惯用的解决方法是什么? 向用户添加一些成员字段?组的成员字段?二者都为它创建一个新类?将这些东西连接在一起需要哪些注释


共 (1) 个答案

  1. # 1 楼答案

    我使用了next architecture

    用户实体类

    @Entity
    @Table(name = "user")
    public class UserEntity implements Serializable {
    
        private Long user_id;
        private String username;
        private String password;
    
        public UserEntity() {
    
        }
    
        public UserEntity(String name, String passwd) {
            username = name;
            password = passwd;
        }
    
        @Column(name = "password", nullable = false)
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Id
        @Column(name = "id")
        @GeneratedValue(strategy = GenerationType.AUTO)
        public Long getUser_id() {
            return user_id;
        }
    
        public void setUser_id(Long user_id) {
            this.user_id = user_id;
        }
    
        @Column(name = "username", nullable = false)
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    }
    

    AuthorityEntity类

    @Entity
    @Table(name = "authority_role")
    public class AuthorityEntity implements Serializable {
    
        private Integer id;
        private String authority;
        private List<UserEntity> people;
    
        public AuthorityEntity() {
        }
    
        public AuthorityEntity(String authString) {
            authority = authString;
        }
    
        @Column(name = "authority", nullable = false)
        public String getAuthority() {
            return authority;
        }
    
        public void setAuthority(String authority) {
            this.authority = authority;
        }
    
        @Id
        @Column(name = "id")
        @GeneratedValue(strategy = GenerationType.AUTO)
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        @ManyToMany(targetEntity = UserEntity.class,
        cascade = {CascadeType.PERSIST, CascadeType.MERGE})
        @JoinTable(name = "authority_role_people",
        joinColumns =
        @JoinColumn(name = "authority_role_id"),
        inverseJoinColumns =
        @JoinColumn(name = "user_id"))
        public List<UserEntity> getPeople() {
            return people;
        }
    
        public void setPeople(List<UserEntity> people) {
            this.people = people;
        }
    }
    

    事实上,这就是spring安全插件的实现方式。 在您的情况下,您可以实现该体系结构,这将对您更有用