7

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

Darth Blue Ray
  • 9,445
  • 10
  • 35
  • 48

2 Answers2

11

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"));
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Parenthesis not required? Other answer includes them – Found some details over at [this answer](http://stackoverflow.com/a/21341663/1146608) – Patrick M Apr 13 '16 at 22:24
2

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);
NPKR
  • 5,368
  • 4
  • 31
  • 48