Set parentheses in Querydsl

1.7k views Asked by At

How is it possible to set parentheses in Querydsl? If I have a complex boolean expression, for example

b1 and b2 and ((b3 or b4) and b5) and ((b6 and b7) or b8)

it would be nice to get a select statement nesting the boolean expression like in this example. The best result with Querydsl I got was

b1 and b2 and (b3 or b4 and b5) and (b5 and b6 or b7)

My code looked like this:

BooleanExpression be = obj.v1.eq(key)
    .and(obj.v2.eq(..))
    .and(obj.v3.goe(..).or(obj.v3.loe(..)).and(obj.v4.eq(..))
    .and(obj.v5.goe(..).and(obj.v5.loe(..)).or(obj.v5.goe(..))
1

There are 1 answers

3
Jan-Willem Gmelig Meyling On

The result that QueryDSL generates is logically equivalent.

However, if you really want the parentheses there, QueryDSL has the WRAPPED operator that wraps an expression within parentheses.

Use:

Expressions.predicate(Ops.WRAPPED, firstExpression.and(secondExpression))