JPA Criteria API IN expression Parameter list

54,072

Solution 1

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

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

Solution 2

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

should do the trick, AFAIK.

Share:
54,072

Related videos on Youtube

user1414341
Author by

user1414341

Updated on November 14, 2020

Comments

  • user1414341
    user1414341 over 3 years

    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?

  • user1414341
    user1414341 almost 12 years
    Great, thanks this works, but I'm using Hibernate and it seems that Hibernate doesn't support empty collection as parameter of javax.persistence.criteria.Expression "in" method parameter. See lists.jboss.org/pipermail/hibernate-issues/2011-December/…
  • JB Nizet
    JB Nizet almost 12 years
    AFAIK, no-one supports them. You should probably short-circuit the query in case an empty list is passed as argument.
  • Miklos Krivan
    Miklos Krivan almost 8 years
    I have found this expression (wrapped in isTrue) raises exception PREDICATE_PASSED_TO_EVALUATION in EclipseLink 2.6.2 but without isTrue wrapping works perfect as predicate in my example probably because "in" returns with Predicate object.
  • Miklos Krivan
    Miklos Krivan almost 8 years
    Instead of "no need" I would say "must not". At least using EclipseLink 2.6.2 for sure. I have tested.
  • jFrenetic
    jFrenetic almost 8 years
    @MiklosKrivan well, both should work, this just looks clearer to me.
  • Miklos Krivan
    Miklos Krivan almost 8 years
    I would expect so as well but unfortunately using EclipseLink 2.6.2 for ORM (I have tried both formula) the isTrue() wrapping raises the mentioned exception. That is why my wording suggestion given. So theoretically "no need" but practically "must not".
  • jFrenetic
    jFrenetic almost 8 years
    @MiklosKrivan will have to check, thanks for clarifying.
  • jFrenetic
    jFrenetic almost 8 years
    @MiklosKrivan can you, please, share your stack trace on pastebin or someplace else, if possible?