Hibernate查询-似乎无法比较所有数据集?

Hibernate查询-似乎无法比较所有数据集?,hibernate,Hibernate,以下是我的数据库结构: Foo: //foo class Set<Students> students; Set<Teachers> teachers; //students Students: Int id; String name; //teachers Teachers: Int id; String name; Criteria

以下是我的数据库结构:

    Foo: //foo class
       Set<Students> students;
       Set<Teachers> teachers;
    //students
    Students:
       Int id;
       String name;
    //teachers
    Teachers:
       Int id;
       String name;


    Criteria criteria = sess.createCriteria(Foo.class ,"foo").
                             createAlias("students", "student").
                             createAlias("teachers","teacher");

    //create and statement for the student id

    Conjunction andStudent = Restriction.conjunction();
    for (Student student : currentFoo.students) {
        andStudent.add(Restrictions.eq("student.id", student.id));
    }
基本上,我想创建一个hibernate查询,其中给定一个Foo列表,我想找到与我要比较的Foo中的所有学生和教师匹配的Foo。因此,如果currentFoo具有学生ID 1,3和教师ID 2,4,我希望查询返回同时包含学生ID 1,3和教师ID 2,4的Foo以及它可能包含的任何其他内容

在我运行查询处理唯一结果并删除currentFoo结果后,我得到0个结果

我确实从currentFoo中为学生删除了一行数据,从id为1,2的两行删除到了id为1的一行,我得到了结果-所以在我看来,问题的一部分是在查询过程中没有比较或读入第二行学生?有人能帮忙吗?谢谢

在这种情况下,您不希望使用eq标准。in标准就是您所需要的。将您的循环替换为构建学生ID集合的循环,然后您可以使用单个未循环限制:

andStudent.add(Restrictions.in("student.id", collectionOfStudentIds));
你甚至可以简单地做到

andStudent.add(Restrictions.in("student", collectionOfStudents));

但是我已经有一段时间没用这个了,所以我忘了它是否有效。

谢谢你的回复。我以前确实使用过Restrictions.in,但如果有任何id匹配,它会返回Foo。例如,如果Foo的id为1,3,而我正在将其与学生id为1,4,5的Foo进行比较,我会将其包含在结果中,即使它不应该是…这就是为什么要使用连词。您需要currentFoo中的studentA和currentFoo中的studentB这样的逻辑,currentFoo中的teacherX和currentFoo中的teacherY这样的逻辑,您可以添加额外的条件,如和学生数=2和教师数=2,以获得所需逻辑的最后一位