Using JPA to make an IN Query
How to query a database table with an IN statement in JPA.
There comes a time in using JPA with your database that you need to do a query with a list of values for a given field. In order to do this you’ll need to define the IN
query as a JPA query. With the @Query
annotation, define the column you need to query with the list of parameters. Reference the parameters with the :parametername
syntax after the IN
keyword.
For a full example see:
public interface MyObjectRepository extends PagingAndSortingRepository<Model, String> {
@Query("SELECT m FROM MyObject m WHERE m.type IN :types")
public List<MyObject> findAllByTypes(List<ObjectType> types);
}
Based on the question at Stackoverflow.
If you have any other ideas on different approaches for this query, comment below.