Java Hibernate条件查询多个条件

Java Hibernate条件查询多个条件,java,hibernate,hibernate-criteria,Java,Hibernate,Hibernate Criteria,在我当前的项目中,我遇到了使用hibernate条件查询获取实体的问题。我拥有以下实体: 教授,其中包含一份学生名单 学生,其中包含作业列表 作业,包含分配给它的学生的id 现在,我想获得所有与教授相关的作业,即教授分配给学生的所有作业 此查询显示了我希望在条件查询中实现的内容 select * from Assigment p, Student a, Professor c where p.studentid = a.id and a.proffid = c.id and c.id = 2

在我当前的项目中,我遇到了使用hibernate条件查询获取实体的问题。我拥有以下实体:

  • 教授,其中包含一份学生名单
  • 学生,其中包含作业列表
  • 作业,包含分配给它的学生的id
现在,我想获得所有与教授相关的作业,即教授分配给学生的所有作业

此查询显示了我希望在条件查询中实现的内容

select * from Assigment p, Student a, Professor c where p.studentid = a.id and a.proffid = c.id and c.id = 2411;

如何使用hibernate criteria API实现此查询?

假设您的表是这样的:

@Entity
public class Professor{
    K id;
    List<Student> students;
}

@Entity
public class Student{
    K profid;
    List<Assignments> assignments;
}

@Entity
public class Assignments{
    K studentid;
}

非常感谢你的回答!如果我想为
professor.id
添加一个
限制。eq
等于1234,即直接将属性与
Long
进行比较,该怎么办?@KevinMeredith Restrictions.eq(“professor.id”,1234L)正在寻找类似的东西!很高兴此解决方案在3年后仍然有效:DcreateCriteria已被弃用。更新答案。
Criteria criteria = currentSession.createCriteria(Professor.class, "professor");
    criteria.createAlias("professor.students", "student");
    criteria.createAlias("student.assigments", "assigment");
    criteria.add(Restrictions.eqProperty("professor.id", "student.profid"));
    criteria.add(Restrictions.eqProperty("assigment.studentid", "student.profid"));
    criteria.add(Restrictions.eq("id", 2411));
return criteria.list();