gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:13 24 4
gpt4 key购买 nike

enter image description here

我有两个表 Role 和 Right,并且 hibernate 创建了 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

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

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()

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

字符串查询=“从用户权限中选择Role_Id,RightName加入role_right on”+ "userrights.RightID = role_right.Right_Id";

但不知道下一步该做什么

最佳答案

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

  • 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 中有很好的解释。 。阅读它。

关于java - 多对多映射后如何使用Hibernate读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10870375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com