JPA Criteria API IN expression Parameter list

user1414341 picture user1414341 · May 24, 2012 · Viewed 52.7k times · Source

Is there a possibility to use a parameter list in Criteria API .in expression?

I have something like this:

    List<Long> list = new ArrayList<Long>();
    list.add((long)1);
    list.add((long)2);
    list.add((long)3);


CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Bewerbung> criteriaQuery = cb.createQuery(Bewerbung.class);
Root<Bewerbung> bewerbung = criteriaQuery.from(Bewerbung.class);

criteriaQuery.select(bewerbung).where(
cb.in(bewerbung.get(Bewerbung_.bewerberNummer)).value(list);

return em.createQuery(criteriaQuery).getResultList();

The expression .value(list) does not work as value() is expecting a paramter of type long not a list. In my case it is not possible to use a subquery. Can anyone help me on this issue?

Answer

jFrenetic picture jFrenetic · Oct 30, 2012

No need to use CriteriaBuilder#isTrue. This should suffice:

criteriaQuery.select(bewerbung)
             .where(bewerbung.get(Bewerbung_.bewerberNummer)
             .in(list));