Search code examples
javajpajpql

JPA / JPQL data list injection


I'm trying to get somthing like this in JPA:

SELECT ...
FROM Entity e 
WHERE e.values IN (:values)

Resulting in the execution of this:

SELECT ...
FROM TABLE 
WHERE VALUES IN (1,2,3,4)

Does anybody know how to do this?

I'll be very grateful.


Solution

  • You can use a list as a parameter, this is an example to show how to set the parameter in the query:

            TypedQuery<Entity> query = em.createQuery("select e from Entity e where e.value IN :values", Entity.class);
            List<Integer> list = Arrays.asList(new Integer[]{1, 2, 3, 4});
            query.setParameter("values", list);
            List<Entity> results = query.getResultList();