title: Spring Boot QueryDSL BooleanExpression - "OR" condition depending on the value in table
body: <p>I use Spring Boot and QueryDSL to combine a sql query and predicate. The problem is that I have to create a predicate to fetch data from table based on email BUT:</p>
  <ol>
  <li>email can be in Freight.sender.email</li>
  <li>OR in Freight.message.senderAddress</li>
  </ol>
  <p>where Freight, Message, Sender are of course tables.
  In table Freight we can have empty sender_id or message_id and depending on this I have to fetch rows by email from <code>Freight.sender.email</code> OR <code>Freight.message.senderAddress</code> (if <code>Freight.sender</code> is null)</p>
  <p>Is it possible to create such a predicate that compares email from request query param with Freight.sender.email and only if Freight.sender.email doesn't exist, then my predicate shold search email in Freight.message.senderAddress</p>
  <pre class="lang-java prettyprint-override"><code>public Predicate build(Map&lt;String, String&gt; filters) {
          return new OptionalBooleanBuilder(Expressions.asBoolean(true).isTrue())
                  .notNullAnd(qFreight.loadingAddress::containsIgnoreCase, filters.get(LOADING_ADDRESS))
                  .notNullAnd(qFreight.unloadingAddress::containsIgnoreCase, filters.get(UNLOADING_ADDRESS))
                  .notNullAnd(qFreight.loadingDate.eq(filters.get(LOADING_DATE) != null ? LocalDate.parse(filters.get(LOADING_DATE)) : now()), filters.get(LOADING_DATE))
                  .notNullAnd(qFreight.unloadingDate.eq(filters.get(UNLOADING_DATE) != null ? LocalDate.parse(filters.get(UNLOADING_DATE)) : now()), filters.get(UNLOADING_DATE))
  //MY ATTEMPT - NOT WORKING:
          .notNullAnd(qFreight.sender.email.eq(filters.get(SENDER_EMAIL)).or(qFreight.emailMessage.senderAddress.eq(SENDER_EMAIL)), filters.get(SENDER_EMAIL))
                  .build();
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>public class OptionalBooleanBuilder {
  
      private BooleanExpression predicate;
  
      public OptionalBooleanBuilder(BooleanExpression predicate) {
          this.predicate = predicate;
      }
  
      public &lt;T&gt; OptionalBooleanBuilder notNullAnd(Function&lt;T, BooleanExpression&gt; expressionFunction, T value) {
          if (nonNull(value)) {
              return new OptionalBooleanBuilder(predicate.and(expressionFunction.apply(value)));
          }
          return this;
      }
  
      public BooleanExpression build() {
          return predicate;
      }
  
      public &lt;T&gt;OptionalBooleanBuilder notNullAnd(BooleanExpression expression, T value) {
          if(nonNull(value)){
              return new OptionalBooleanBuilder(predicate.and(expression));
          }
          return this;
      }
  }
  </code></pre>
  <p><strong>UPDATE</strong></p>
  <p>After suggestion from:</p>
  <pre><code>private Predicate addPredicate(OptionalBooleanBuilder builder, String email) {
      if (nonNull(email)) {
          return builder.notNullAnd(qFreight.sender.email.coalesce(qFreight.emailMessage.senderAddress.eq(email)).asBoolean(), email).build();
  
      return builder.build();
  }
  </code></pre>
  <p>I get error:</p>
  <pre><code>    antlr.NoViableAltException: unexpected AST node: (
          at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2169) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
          at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2089) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
          at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:827) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
          at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:621) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
          at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:325) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
          at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:273) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
          at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
          at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  
  
  2020-12-24 00:45:35.535 ERROR 16068 --- [nio-9090-exec-8] p.a.m.s.filter.JwtAuthorizationFilter    : Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: ( near line 3, column 52 [select freight
  from pl.appwise.mtf.freight.domain.model.Freight freight
  where ?1 = ?1 and freight.user.id = ?2 and coalesce(freight.sender.email, freight.emailMessage.senderAddress = ?3)
  order by freight.loadingDate desc]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: ( near line 3, column 52 [select freight
  from pl.appwise.mtf.freight.domain.model.Freight freight
  where ?1 = ?1 and freight.user.id = ?2 and coalesce(freight.sender.email, freight.emailMessage.senderAddress = ?3)
  order by freight.loadingDate desc]
  2020-12-24 00:45:37.906  INFO 16068 --- [   scheduling-1] ilAccoun
  </code></pre>