如何在Hibernate下加入两个不同的标准? [英] How to join two different criterias under Hibernate?
问题描述
学生{
Classe currentClasse;
Set< Assignement> assignements;
}
Classe {
Set< Assignement> assignements;
SchoolYear schoolYear;
}
Assignement {
Classe class;
学生学生;
布尔传球;
}
基本上,学生有一个当前班级和一个分配列表(全部他们被分配到的类)。
我的问题是,在hibernate下,我想知道所有没有类的学生(<$ c $分配给属于学校年1但具有 pass
组的类的c <空> ) OR 到 true
我设法做出两个单独的标准来获得我想要的结果:
session.createCriteria(Student.class)
.add(Restrictions.isNull(classeActuelle))
和
session.createCriteria(Student .class)
.createAlias(assignements,a)
.add(Restrictions.eq(a.pass,Boolean.TRUE)
.createAlias(a.classe (c.schoolYear,1))
但我不知道如何团结thos如果你想创建一个 如果您需要用许多 在您的具体情况中你可以用两个 以下代码使用 I have three tables in my database, a Student, a Classe, and an Assignement. The POJOs are as the following: Basically, a student has a current class, and a list of assignements (all the classes he's been assigned to). My problem is that under hibernate, I'd like to know all the students who do not have a classe ( I managed to make two separate criterias to get what I want: and but I don't know how I can "unite" those two criterias with an or... If you want to create an If you need to create an The In your specific case, you can create an outer The following code uses both the
这篇关于如何在Hibernate下加入两个不同的标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!或者
或者 表达
与许多 org.hibernate.criterion.Disjunction
。使用这个对象相当于,但比使用几个OR限制更方便。要获得一个 Disjunction
对象,请调用 Restrictions.disjunction()
。
Criterion
对象创建 AND
表达式,那么您可以使用 org.hibernate.criterion.Conjunction
。 Restrictions.conjunction()
方法返回连接
。
Disjunction
和 Conjunction
类提供 add()
方法分别在标准之间应用OR或AND。您的情况:
Criterion
创建一个外部的 Disjunction
:
学生
没有课程的学生;
连接
与两个标准
:
学生
s分配给属于 schoolYear
的
; classe
学生 s
设置为 true
。
示例代码:
Disjunction
和 Conjunction
对象来构造标准c = session.createCriteria(Student.class)
.createAlias(assignements,必需的标准。
a)
.createAlias(a.classe,c);
Disjunction或= Restrictions.disjunction();
or.add(Restrictions.isNull(classeActuelle));
连接和= Restrictions.conjunction();
and.add(Restrictions.eq(a.pass,Boolean.TRUE)
and.add(Restrictions.eq(c.schoolYear,1))
or.add(and);
c.add(or);
//你可以使用你的标准c现在
Student{
Classe currentClasse;
Set<Assignement> assignements;
}
Classe{
Set<Assignement> assignements;
SchoolYear schoolYear;
}
Assignement{
Classe classe;
Student student;
Boolean pass;
}
is null
) OR who are assigned to a class belonging to the schoolyear "1" but have pass
set to true
session.createCriteria(Student.class)
.add(Restrictions.isNull("classeActuelle"))
session.createCriteria(Student.class)
.createAlias("assignements", "a")
.add(Restrictions.eq("a.pass", Boolean.TRUE)
.createAlias("a.classe","c")
.add(Restrictions.eq("c.schoolYear", "1"))
OR
expression with many Criterion
objects, you can use an instance of org.hibernate.criterion.Disjunction
. Using this object is equivalent to, but more convenient than, using several OR restrictions. To obtain a Disjunction
object, call Restrictions.disjunction()
.AND
expression with many Criterion
objects, you can use an object of org.hibernate.criterion.Conjunction
. The Restrictions.conjunction()
method returns a Conjunction
.Disjunction
and Conjunction
classes provide add()
methods to apply an OR or an AND, respectively, between the criteria.Your case:
Disjunction
with two Criterion
:
Student
s who do not have a class;Conjunction
with two Criterion
:
Student
s assigned to a classe
belonging to the schoolYear
"1"
;Student
s that have pass
set to true
.Sample code:
Disjunction
and Conjunction
objects to construct the necessary criteria.Criteria c = session.createCriteria(Student.class)
.createAlias("assignements", "a")
.createAlias("a.classe","c");
Disjunction or = Restrictions.disjunction();
or.add(Restrictions.isNull("classeActuelle"));
Conjunction and = Restrictions.conjunction();
and.add(Restrictions.eq("a.pass", Boolean.TRUE)
and.add(Restrictions.eq("c.schoolYear", "1"))
or.add(and);
c.add(or);
// you can use your criteria c now