8

How do I express in QueryDSL where clause in form:

 WHERE (E1 AND E2) OR (E3 AND E4) 

E1..E4 are arbitrary boolean expressions. The point is to have a query started within parenthesis, thus (E1 AND E2).

csviri
  • 1,159
  • 3
  • 16
  • 31

1 Answers1

11
where(e1.and(e2).or(e3.and(e4)))
ponzao
  • 20,684
  • 3
  • 41
  • 58
  • Its true that this is equivalent logical formula as above, but not the same. What if I want to generate an SQL statement that is starting with parenthesis, although its logically equivalent to formula without them? – csviri May 14 '12 at 14:17
  • 1
    Querydsl guarantees that expressions are serialized in the correct way to SQL, so you needn't worry about paranthesis – Timo Westkämper May 28 '12 at 20:22
  • 2
    "I don't need to worry" sounds a bit tricky for me. I never know if `where(e1.or(e2).and(e3))` means `(e1 or e2) and e3)` or `e1 or (e2 and e3)`. I was really surprised that `where(e1.or(e2).and(e3.or(e4)))` produced `(e1 or e2) and (e3 or e4)` which I actually wanted. But how did the first OR happen to be in parenthesis? Based on what? Does `...and(e3.or(e4))` makes any difference to `...and(e3).or(e4)` then? We generally review our queries in log, but I'd love to find something more concrete about this serialization. – virgo47 Mar 06 '13 at 07:51
  • 1
    @TimoWestkämper Yes, it does help. I also found BooleanBuilder in the meantime - which is answer to anything more complicated I guess. First I have to say, that I work with Querydsl around 4 months - it is one of the best libs I've ever encountered (usability, easy adoption, ...) - but the whole domain has inherent complexity. Sometimes I want to use javadoc on "where", but since it is generated, it doesn't help (understandably). I couldn't find exact explanation what x1.or.x2.or.x3.and.x4.whateveroperation.x5 does. However, Timo, you do a great job on any forum I saw. Thank you very much. – virgo47 Mar 27 '13 at 20:18
  • 6
    @TimoWestkämper i am having problems when I do that. Thru my debugger, the generated jpa QL is shown as "a and b or c and d". i want "(a and b) or (c and d)" parenthesis is missing. QueryDSL 3.6.3. – Titi Wangsa bin Damhore May 11 '15 at 06:37