本文介绍了如何在Hibernate下加入两个不同的标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库里有三张表,一个是学生,一个是Classe,一个是Assignement。 POJO如下所示:

 学生{
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如果你想创建一个 或者 或者 表达与许多。使用这个对象相当于,但比使用几个OR限制更方便。要获得一个 Disjunction 对象,请调用。

如果您需要用许多 Criterion 对象创建 AND 表达式,那么您可以使用。 方法返回连接。



Disjunction 和 Conjunction 类提供 add()方法分别在标准之间应用OR或AND。您的情况:

在您的具体情况中你可以用两个 Criterion 创建一个外部的 Disjunction :


  • 学生没有课程的学生;

  • a 连接与两个标准:


    • 学生 s分配给属于 schoolYear 的 classe ;

    • 学生 s pass 设置为 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现在


    I have three tables in my database, a Student, a Classe, and an Assignement. The POJOs are as the following:

    Student{
        Classe currentClasse;
        Set<Assignement> assignements;
    }
    
    Classe{
        Set<Assignement> assignements;
        SchoolYear schoolYear;
    }
    
    Assignement{
        Classe classe;
        Student student;
        Boolean pass;
    }
    

    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 (is null) OR who are assigned to a class belonging to the schoolyear "1" but have pass set to true

    I managed to make two separate criterias to get what I want:

    session.createCriteria(Student.class)
           .add(Restrictions.isNull("classeActuelle"))
    

    and

    session.createCriteria(Student.class)
           .createAlias("assignements", "a")
           .add(Restrictions.eq("a.pass", Boolean.TRUE)
           .createAlias("a.classe","c")
           .add(Restrictions.eq("c.schoolYear", "1"))
    

    but I don't know how I can "unite" those two criterias with an or...

    解决方案

    If you want to create an 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().

    If you need to create an AND expression with many Criterion objects, you can use an object of org.hibernate.criterion.Conjunction. The Restrictions.conjunction() method returns a Conjunction.

    The Disjunction and Conjunction classes provide add() methods to apply an OR or an AND, respectively, between the criteria.

    Your case:

    In your specific case, you can create an outer Disjunction with two Criterion:

    • Students who do not have a class;
    • a Conjunction with two Criterion:
      • Students assigned to a classe belonging to the schoolYear "1";
      • Students that have pass set to true.

    Sample code:

    The following code uses both the 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
    

    这篇关于如何在Hibernate下加入两个不同的标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-21 03:36