我有两个表 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";
但不知道下一步该怎么做