简体   繁体   English

Hibernate条件查询多个条件

[英]Hibernate criteria query multiple criteria

In my current project I've faced a problem of getting entities with hibernate criteria query. 在我目前的项目中,我遇到了使用hibernate条件查询获取实体的问题。 I have the following entities: 我有以下实体:

  • Professor, which contains a list of students 教授,其中包含一份学生名单
  • Student, which contains a list of assignments. 学生,其中包含作业列表。
  • Assignment, which contains id of student to which it is assigned to. 分配,包含分配给它的学生的ID。

Now, I want to get all assignments relative to the professor, ie all assignments Professor assigned to his students. 现在,我希望获得与教授相关的所有作业,即教授分配给他的学生的所有作业。

This query shows what I want to implement in criteria query. 此查询显示我要在条件查询中实现的内容。

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

How can I implement this query using hibernate criteria API? 如何使用hibernate criteria API实现此查询?

suppose your tables like that: 假设你的表是这样的:

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

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

@Entity
public class Assignments{
    K studentid;
}

simple sample by using alias: 使用别名的简单示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM