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

后端 未结 2 1101
-上瘾入骨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);
    
    0 讨论(0)
  • 2021-01-31 01:11
    Criteria criteriaObj = sessionselectreply.createCriteria(TaskReplyVO.class,"taskreply")
                    .createAlias("taskreply.objTaskView", "taskview")
                    .createAlias("objMailTo", "mailto")
                    .add(Restrictions.eq("taskview.longTaskId", taskid))
                    .add(Restrictions.eq("mailto.longuserid", userid));
    
    0 讨论(0)
提交回复
热议问题