I have a query method in my JpaRepository
Page<Course> findDistinctCourseByAttrsInAllIgnoreCase(Set<String> a, Pageable page);
to find Course
objects by their instance variable Set<String> attrs
. Given a Set a
with "foo" and "bar", I want to find Course
s whose attrs
contain BOTH "foo" and "bar", i.e. an intersection of Course
s with "foo" and those with "bar". This method above returns a union.
Is there a way to do this with JpaRepository queries or do I have to make multiple calls and find the intersection myself?
Something like this should make it:
and you call it like this:
In the unlikely case that you know the number of
a
s up front you could combine multiple constraints withAnd
:But even if the precondition holds that would be very ugly.
So the next best option is probably a Specification and a factoryMethod constructing the Specification from a
Set<String>
or from varargs.Your repository needs to extend
JpaSpecificationExecutor
. You would call it like thisAnd the factory method would look something like this: