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

こ雲淡風輕ζ 提交于 2019-12-02 16:50:54

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);
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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!