Say I have CustomerQueryInfo
bean with the following properties:
I want to perform a "QueryDSL" search using this an object of this type that will return a list of customers List<Customer>
.
If one of the fields of CustomerQueryInfo
is null
, I don't want to use it in the search. Thus a CustomerQueryInfo
object with all three fields set to null
will return all customers.
I am looking for best practices to perform such a search with QueryDSL.
Is something like this OK:
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);
}
then:
return query.from(customer).where(isFirstNameLike(customerQueryInfo.getFirstName).and(isLastNameLike(customerQueryInfo.getLastName).and(isStatusEq(customerQueryInfo.getStatus))).list;
BooleanExpression
that evaluates to true?How do I return a BooleanExpression that evaluates to true?
BooleanExpression alwaysTrue = Expressions.asBoolean(true).isTrue();