Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Criteria API IN expression Parameter list

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?

like image 885
user1414341 Avatar asked May 24 '12 07:05

user1414341


People also ask

What is JPA Criteria API?

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.

Is Criteria API deprecated?

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.

How do you write a subquery in CriteriaBuilder?

where(criteriaBuilder.in(path). value(subquery)); TypedQuery<Object> typedQuery = entityManager. createQuery(select); List<Object> resultList = typedQuery. getResultList();


2 Answers

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

criteriaQuery.select(bewerbung)              .where(bewerbung.get(Bewerbung_.bewerberNummer)              .in(list)); 
like image 83
jFrenetic Avatar answered Oct 14 '22 12:10

jFrenetic


cb.isTrue(bewerbung.get(Bewerbung_.bewerberNummer).in(list)); 

should do the trick, AFAIK.

like image 28
JB Nizet Avatar answered Oct 14 '22 14:10

JB Nizet