Jpa named query: named bind variables for list

if you have namedquery with a list like:

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

Is it possible to list a list with bind variable names so that you set what you want:

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

Any suggestions are welcome.

+7
source share
2 answers

Yes it is possible. Just do it as 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")); 
+11
source

Use this method

 @NamedQuery(name="selection" , query=" SELECT x FROM Employee x WHERE x.name IN (:availableCollection)") namesCollection // conatains your Lsit of names query.setParameterList('availableCollection', namesCollection); 
+2
source

All Articles