I would like to use a parameter expression as part of an in clause. I would like to query for a list of Foos that have a Bar that is in a set of Bars. Is this possible?
With in
expressions you can only use primitive comparison types, so you need to make a join and compare a field of a primitive type (here I have used Integer id
):
Root foo = cq.from(Foo.class);
Join bar = foo.join(Foo_.bar);
ParameterExpression bars = cb.parameter(Collection.class);
cq.where(bar.get(Bar_.id).in(bars));
TypedQuery tq = em.createQuery(cq);
Collection barsParameter = new ArrayList ();
barsParameter.add(1);
List resultList = tq.setParameter(bars, barsParameter).getResultList();