如何在多对多映射后使用Hibernate读取数据

时间:2012-06-03 12:38:02

标签: java sql hibernate hql

enter image description here

我有两个表Role和Right,并且hibernate创建了1个表role_right作为Many to Many映射的结果。

现在我想分别得到与 RoleName 对应的所有 RightName

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

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

data analyst--> data analysis, logout, login

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

UserRole类

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

UserRole类

@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”;

但不知道该怎么做

1 个答案:

答案 0 :(得分: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 documentation中得到了很好的解释。阅读它。