Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a QueryDSL BooleanExpression that evaluates to true
    text
    copied!<p>Say I have <code>CustomerQueryInfo</code> bean with the following properties:</p> <ul> <li>String firstName</li> <li>String lastName </li> <li>StatusEnum status</li> </ul> <p>I want to perform a "<strong>QueryDSL</strong>" search using this an object of this type that will return a list of customers <code>List&lt;Customer&gt;</code>.</p> <p>If one of the fields of <code>CustomerQueryInfo</code> is <code>null</code>, I don't want to use it in the search. Thus a <code>CustomerQueryInfo</code> object with all three <strong>fields set to <code>null</code></strong> will return <strong>all customers</strong>.</p> <p>I am looking for best practices to perform such a search with QueryDSL.</p> <p>Is something like this OK:</p> <pre><code>private BooleanExpression isFirstNameLike(String firstName){ if(firstName==null) return true BooleanExpression somehow; return QCustomer.customer.firstName.like(firstName); } private BooleanExpression isStatutEq(StatusEnum status){ if(status==null) return true BooleanExpression somehow; return QCustomer.customer.status.eq(status); } </code></pre> <p>then:</p> <pre><code>return query.from(customer).where(isFirstNameLike(customerQueryInfo.getFirstName).and(isLastNameLike(customerQueryInfo.getLastName).and(isStatusEq(customerQueryInfo.getStatus))).list; </code></pre> <ol> <li>How do I return a <code>BooleanExpression</code> that evaluates to true?</li> <li>If the above approach is not advisable, then what is the recommended best practice?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload