I'm using the JPA Query Language for the first time and have run into a problem. I'm trying to do a "select where in (list of values)" query, but can't figure out the syntax to avoid hard-coding the list of values.
Replacing the hard-coded values with a parameter and an array gives me a class cast exception:
I'm using JDK 1.5, Hibernate 3, and JPA.
Can anyone give me some pointers on how to do this? (Or a better way to accomplish it?)
absolutely there is a solution two ways you can achieve this, as mentioned by Debu Panda you can create a comma separated String and dynamically add it to a query
String statesCode= "'A', 'E'";
select t from Thing t where t.status in (statesCode)")
List<String> inList = new ArrayList<String>();
select t from Thing t where t.status in (:statesCode)")
query.setParameter("statesCode", inList );
Hope this helps.
Joined: May 01, 2008
Also dont forget that if your IN parameter has numeric data type like Long, smallInt, BigInt etc then you will need to create the list of the appropriate data type instead of Strings. Hope makes sense.