Blessed Geek
Blessed Geek

Reputation: 21654

JPQL - inserting list of items for IN

In JPA, in creating the query, we could perform

(" ... where a.name = :name ...")
.setParameter("name", user.getName());

However, I am hoping if there might be some way in JPQL that I could do:

(" ... where a.name in (:namelist) ...")
.setParameter("namelist", (List<String>)names);

or

" ... where a.name in (:namelist) ...")
.setParameter("namelist", (String[])names);

Does not have to be exactly how I exemplified above. I am looking for any convenient way to build an IN (or NOT IN) list into the query. Any form of suggestions will be welcome. Perhaps, there is an Apache util?

Otherwise, I would have to construct the query and iteratively insert the items into the IN list.

Upvotes: 1

Views: 362

Answers (1)

fdreger
fdreger

Reputation: 12495

Starting with JPA2 it should work out of the box for:

select a from Something a where a.name in :param

where :param is a collection of the proper type. Like:

List<Product> products =
    em.createQuery("select p from Product p where p.productId in :ids")
    .setParameter("ids", Arrays.asList(980001, 980032, 986712)).getResultList();

Upvotes: 1

Related Questions