I am using Postgres. A filter query something like this:
SELECT * FROM Entity E
WHERE E.field1 = ?
AND E.field2 = ?
AND E.field3 = ?
AND E.field4 = IN (?field4[])
?field1 and ?field2 are mandatory fields so they will be present always. ?field3 and ?field4 are optional. I would like to use AND E.field3 = ? & AND E.field4 = IN (?field4[]) only when the value of ?field3 and ?field4[] is present, and ignore otherwise
SELECT * FROM Entity E WHERE E.field1 = ? AND E.field2 = ? AND (E.field3 = ? OR E.field3 = 'default_val') AND (E.field4 = IN (?field4[]) OR E.field4 = 'default_val')JpaSpecificationExecutorrepository methodfindAll(Specification<T> spec, Pageable pageable). This solution allows you to extend the parameters list using the same repository and service API. I have answered a similar question here stackoverflow.com/questions/58240024/…