Darth Blue Ray
Darth Blue Ray

Reputation: 9745

jpa named query : named bind variables for list

if you have a namedquery with a list like :

@NamedQuery(name="selection" , query=" SELECT x FROM Employee x WHERE x.name IN ('Jack', 'Jill')")

is it possible to make the list to named bind variables so you set what you want with :

q.setParameter( .......  );

Any suggestions would be welcome

Upvotes: 7

Views: 8271

Answers (2)

NPKR
NPKR

Reputation: 5506

Use this way

@NamedQuery(name="selection" , query=" SELECT x FROM Employee x WHERE x.name IN (:availableCollection)") 


namesCollection // conatains your Lsit of names

query.setParameterList('availableCollection', namesCollection);

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 692013

Yes, it's possible. Just do it like for any other parameter:

@NamedQuery(name="selection" , query=" SELECT x FROM Employee x WHERE x.name IN :names")

q.setParameter("names", Arrays.asList("Jack", "Jill"));

Upvotes: 11

Related Questions