javaspringspring-dataspring-data-jpaquery-by-example

Query by Example Spring Data - In Clause?


As per Spring Docs, I can write exact matching only for QBE. I need exact matching only but among a set of values (IN clause of query).

e.g.

Person p = new Person();
p.setId(); // need to match among set of ids.
Example.of(p);

Is this somehow achievable with QBE or am I totally down the wrong path?

Something like :

Page<S> findByIdIn(List<Integer> ids, Example<S> e, Pageable p)

best of both worlds?

What I really need, dynamic query based on multiple fields (in possible combinations, say id in (1,2,4), status=open, appointmentDate < today) along with pagination and sorting. Is specification the only way to go apart from native query?


Solution

  • I need exact matching only but among a set of values (IN clause of query).

    So you need something other than exact matching. You can't possibly store a set of IDs in the ID property of your Person. QBE is clearly not the right tool for the job.

    You can use Specifications, the Criteria API directly, QueryDSL, a dynamically composed JPQL query, or whatever other solution, but not QBE.