Search code examples
hibernatehibernate-criteria

Hibernate criteria query for ((C1 and C2) or C3) and C4



        Predicate C= cb.conjunction();
        C.getExpressions().add(C1);
        C.getExpressions().add(C2)
        Predicate orCondition = cb.disjunction();
        orCondition.getExpressions().add(C3);
        orCondition.getExpressions().add(C);
        C4.getExpressions().add(orCondition);

But it is not working as expected.

I have tried with Criterion also,but it is could not add Criterion to hibernate Expressions. Also it is deprecated API.So,any help would be appreciated.


Solution

  • Shouldn't it rather be something like

    Predicate innerAnd = cb.conjunction();
    innerAnd.getExpressions().add(C1);
    innerAnd.getExpressions().add(C2);
    
    Predicate or = cb.disjunction();
    or.getExpressions().add(innerAnd);
    or.getExpressions().add(C3);
    
    Predicate outerAnd = cb.conjunction();
    outerAnd.getExpressions().add(or);
    outerAnd.getExpressions().add(C4);
    

    Your code rather leads to something like

    C1 and C2 and C3
    

    and I don't see C4 in your example.