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

Java 使用Hibernate(注释)创建具有权限的简单用户/组模型,java,hibernate,entity-framework,relational-database,Java,Hibernate,Entity Framework,Relational Database,这很可能是一个非常基本的问题,但是: 基本上,用户实体有一个Id和一个特权枚举。 该组有一个id和一个名称 我想知道如何建立一个关系模型,其中一个用户可以在多个组中,在不同的组中拥有不同的权限级别。当然,每个小组都可以有多个成员 我用Hibernate惯用的解决方法是什么? 向用户添加一些成员字段?组的成员字段?二者都为它创建一个新类?将这些东西连接在一起需要哪些注释?我使用了下一个体系结构 用户实体类 @Entity @Table(name = "user") public class Use

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

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

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

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

我使用了下一个体系结构

用户实体类

@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;
    }
}
@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;
    }
}
AuthorityEntity类

@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;
    }
}
@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;
    }
}
@实体
@表(name=“authority\u role”)
公共类AuthorityEntity实现可序列化{
私有整数id;
私人字符串管理机构;
私人名单;
公共权威实体(){
}
公共授权实体(字符串authString){
authority=authString;
}
@列(name=“authority”,nullable=false)
公共字符串getAuthority(){
返回权限;
}
公共权限(字符串权限){
这个。权威=权威;
}
@身份证
@列(name=“id”)
@GeneratedValue(策略=GenerationType.AUTO)
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
@ManyToMany(targetEntity=UserEntity.class,
cascade={CascadeType.PERSIST,CascadeType.MERGE})
@JoinTable(name=“authority\u role\u people”,
连接柱=
@JoinColumn(name=“authority\u role\u id”),
反向连接柱=
@JoinColumn(name=“user\u id”))
公共列表getPeople(){
还人,;
}
公众人物(列出人物){
这个。人=人;
}
}
事实上,这就是spring安全插件的实现方式。
在您的情况下,您可以实现该体系结构,这将对您更有用。

我使用了下一个体系结构

用户实体类

@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;
    }
}
@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;
    }
}
AuthorityEntity类

@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;
    }
}
@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;
    }
}
@实体
@表(name=“authority\u role”)
公共类AuthorityEntity实现可序列化{
私有整数id;
私人字符串管理机构;
私人名单;
公共权威实体(){
}
公共授权实体(字符串authString){
authority=authString;
}
@列(name=“authority”,nullable=false)
公共字符串getAuthority(){
返回权限;
}
公共权限(字符串权限){
这个。权威=权威;
}
@身份证
@列(name=“id”)
@GeneratedValue(策略=GenerationType.AUTO)
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
@ManyToMany(targetEntity=UserEntity.class,
cascade={CascadeType.PERSIST,CascadeType.MERGE})
@JoinTable(name=“authority\u role\u people”,
连接柱=
@JoinColumn(name=“authority\u role\u id”),
反向连接柱=
@JoinColumn(name=“user\u id”))
公共列表getPeople(){
还人,;
}
公众人物(列出人物){
这个。人=人;
}
}
事实上,这就是spring安全插件的实现方式。
在您的情况下,您可以实现该体系结构,这将对您更有用。

Afaiu您的
AuthorityEntity
就是我所说的
Group
?我原来的问题仍然存在,我不仅有一个用户列表,还有一个具有权限级别的用户列表。您的
AuthorityEntity
就是我所说的
?我最初的问题仍然存在,我不仅有一个用户列表,而且还有一个具有权限级别的用户列表。