1

在此处输入图像描述

我有两个表 Role 和 Right,由于多对多映射,休眠创建了 1 个表 role_right。

现在我想分别获取与RoleName对应的所有RightName

admin--> create user, data entry, login, logout

data entry operator--> data entry, logout, login

data analyst--> data analysis, logout, login

帮我解决这个问题。这是我的代码

用户角色类

@Entity
@Table(name="UserRole")
public class UserRole implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="RoleID", unique=true, nullable=false)
    private int roleId;
    @Column(name="RoleName", nullable=false)
    private String roleName;
    @Column(name="RoleRight")
    private String roleRight;

    @ManyToMany
    @JoinTable(name="Role_Right",joinColumns=@JoinColumn(name="Role_Id"),
            inverseJoinColumns=@JoinColumn(name="Right_Id"))
    private Collection<UserRights> userRoleMapping = new ArrayList<UserRights>();

    public Collection<UserRights> getUserRoleMapping() {
        return userRoleMapping;
    }

    public void setUserRoleMapping(Collection<UserRights> userRoleMapping) {
        this.userRoleMapping = userRoleMapping;
    }

    public String getRoleRight() {
        return roleRight;
    }

    public void setRoleRight(String roleRight) {
        this.roleRight = roleRight;
    }

    public int getRoleId() {
        return roleId;
    }

    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    } 
}

用户角色类

@Entity
@Table(name="UserRights")
public class UserRights implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="RightID", unique=true, nullable=false)
    private int rightId;
    @Column(name="RightName", nullable=false)
    private String rightName;

    @ManyToMany(mappedBy="userRoleMapping")
    private Collection<UserRole> userRightsMapping = new ArrayList<UserRole>();

    public Collection<UserRole> getUserRightsMapping() {
        return userRightsMapping;
    }

    public void setUserRightsMapping(Collection<UserRole> userRightsMapping) {
        this.userRightsMapping = userRightsMapping;
    }

    public int getRightId() {
        return rightId;
    }

    public void setRightId(int rightId) {
        this.rightId = rightId;
    }

    public String getRightName() {
        return rightName;
    }

    public void setRightName(String rightName) {
        this.rightName = rightName;
    } 
}

我试过这样的事情

session.getUserRightsMapping()

这个查询非常接近我想要的

String query = "select Role_Id, RightName FROM userrights join role_right on " + "userrights.RightID = role_right.Right_Id";

但不知道下一步该怎么做

4

1 回答 1

1

首先,为了让事情更清楚,我将从重命名您的实体和字段开始:

  • UserRole,具有名为...的权利集合rights
  • UserRight(不带 s),带有一组名为...的角色roles

现在,最简单的做法是获取所有角色,并在单个查询中获取每个角色的权限:

String hql = "select role from UserRole role left join fetch role.rights";
List<UserRole> roles = session.createQuery(hql).list()
// now you can iteratr through the roles, and for each role, iterate through its rights

如果您真的只想拥有所有 role_name/right_name 关联,请使用以下代码:

String hql = "select role.name, right.name from UserRole role"
             + " inner join role.rights right";
List<Object[]> rows = session.createQuery(hql).list();

并且返回列表的每个 Object[] 元素都包含两个元素:角色名称和权限名称。请注意,此查询不会返回没有任何权限的角色。

所有这些都在Hibernate 文档中得到了很好的解释。阅读。

于 2012-06-03T19:43:54.320 回答