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?
The Criteria API is a predefined API used to define queries for entities. It is the alternative way of defining a JPQL query. These queries are type-safe, and portable and easy to modify by changing the syntax.
The Criteria API allows us to build up a criteria query object programmatically, where we can apply different kinds of filtration rules and logical conditions. Since Hibernate 5.2, the Hibernate Criteria API is deprecated, and new development is focused on the JPA Criteria API.
where(criteriaBuilder.in(path). value(subquery)); TypedQuery<Object> typedQuery = entityManager. createQuery(select); List<Object> resultList = typedQuery. getResultList();
No need to use CriteriaBuilder#isTrue
. This should suffice:
criteriaQuery.select(bewerbung) .where(bewerbung.get(Bewerbung_.bewerberNummer) .in(list));
cb.isTrue(bewerbung.get(Bewerbung_.bewerberNummer).in(list));
should do the trick, AFAIK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With