I'm trying to set a parameter in my query, for example:
select * from Cars where Cars.color NOT IN (:color_params)
And when I'm adding the parameter in my JavaClass is like:
...
query.setParameter("color_params", "RED,BLUE");
...
And this is not working, is only working with only one parameter.
I've tried with "'RED','BLUE'"
and is not working to.
If I put my parameters in the query is working for example:
select * from Cars where Cars.color NOT IN ('RED','BLUE')
What I'm doing wrong!?
Thanks in advance
You are supposed to pass a List.
List<String> colors = ....;
String query = "select * from Cars where Cars.color NOT IN (:color_params)";
Map<String, Object> params = new HashMap<String, Object>();
params.put("color_params", colors);
// ... execute the query with the param.
You could also do:
query.setParameter("color_params", colors);
As a general rule, it is often prefered to pass parameters to a fixed query, instead of customizing the String. The advantages could be:
You must pass in a list of strings, not a single string. JPA doesn't parse your values, you must split them yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With