Hibernate Criteria 连接两个表

Hibernate Criteria join two tables

我有 3 个 table: 人:身份证、姓名、姓氏 团队:ID、姓名、人员、角色 角色:ID,位置 其中 TEAM 可以有一个 PERSON

列表

我需要使用 Hibernate Criteria 创建一个查询,如下所示: select * 来自人 p,团队 t 其中 t.ROLE = "myRole" 和 t.PERSON = p.id

我想获取 TEAM 中具有给定 ROLE 的人员列表。 你能帮助我吗?谢谢

团队有人员名单。因此,在 Team table 中不应包含 Person 列,而应在 Person table 中包含 team_id 列。我假设 Role 与 Person.Thus 相关联,您的 tables 应该如下所示:

人:身份证、姓名、姓氏,TEAM_ID,ROLE_ID 团队:ID,NAME Role:ID,位置

那么获取给定团队中具有给定角色的人员列表的查询是:

Select * from Person p, Team t, Role r where p.team_id=t.id and p.role_id=r.id and r.position = givenPosition and t.name=givenTeam

在标准中

`Criteria c = session.createCriteria(Person.class, "p");
c.createAlias("p.team", "t");
c.createAlias("p.role", "r");
c.add(Restrictions.eq("t.name", givenTeam));
c.add(Restrictions.eq("r.position", givenPosition ));`