How to use Hibernate Criteria objects for multiple and/or conditions

后端 未结 2 1130
-上瘾入骨i
-上瘾入骨i 2021-01-31 00:40

I need to create a Hibernate criteria restriction that ors 3 Conditions. The problem is that the last condition is acutally to conditions using the AND operator.

My fir

2条回答
  •  情话喂你
    2021-01-31 01:06

    A sequence of restrictions linked with or is called a disjunction. A sequence of restrictions linked with and is called a conjunction.

    So, what you need is

    • one conjunction for your third condition
    • one disjunction to link the first, second, and third conditions:

    So here it goes:

    Criterion startInRange = Restrictions.between("expectedStartCanonicDate", rangeStart, rangeEnd);
    
    Criterion endInRange = Restrictions.between("expectedCompletionCanonicDate", rangeStart, rangeEnd);
    
    Criterion thirdCondition = 
        Restrictions.conjunction().add(Restrictions.le("expectedStartCanonicDate", rangeStart))
                                  .add(Restrictions.ge("expectedCompletionCanonicDate", rangeEnd));
    
    Criterion completeCondition = 
        Restrictions.disjunction().add(startInRange)
                                  .add(endInRange)
                                  .add(thirdCondition);
    
    criteria.add(completeCondition);
    

提交回复
热议问题