You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When specifying named parameters and using a NamedParameterJdbcTemplate, if you want to replace an "IN" clause argument with a series of values you must supply a Collection.
Example:
Map<String, ?> params = newHashMap<>();
params.put("ages", Arrays.asList("30", "31");
template.queryForList("SELECT name FROM Person WHERE age IN (:ages)", params, String.class);
This will translate to: SELECT name FROM Person WHERE age IN (?, ?)
with those "?" replaced with 30 and 31 respectively.
If, however, you specify ages param as an array, or as some other kind of Iterable, this does not work: the parameter is translated into a single String SQL param value using the array or IterabletoString() output.
IMHO this is not intuitive, especially if you're using an array (an array of enum values, for instance). It took me quite a lot today to discover why a query was not producing the expected results.
The text was updated successfully, but these errors were encountered:
I'm afraid that this has caused a regression in Spring Data JDBC, potentially affecting other code as well: An array argument has special semantics in JDBC, with the SQL type ARRAY inferred from it by default, so we'll have to reduce this to Iterable handling but keep array handling as before.
jhoeller
changed the title
NamedParameterJdbcTemplate: treat parameter arrays and Iterables like Collections
NamedParameterJdbcTemplate: treat Iterable parameters like Collections
Jun 12, 2019
Hi Juergen,
I didn't know about the SQL type ARRAY. Thanks for the inclusion of Iterable. Maybe a note somewhere in the NamedParameterJdbcTemplate Javadoc would help to avoid confusion when using arrays.
Uh oh!
There was an error while loading. Please reload this page.
Affects: 4.3.18
When specifying named parameters and using a
NamedParameterJdbcTemplate
, if you want to replace an "IN" clause argument with a series of values you must supply aCollection
.Example:
This will translate to:
SELECT name FROM Person WHERE age IN (?, ?)
with those "?" replaced with 30 and 31 respectively.
If, however, you specify
ages
param as an array, or as some other kind ofIterable
, this does not work: the parameter is translated into a single String SQL param value using the array orIterable
toString()
output.IMHO this is not intuitive, especially if you're using an array (an array of enum values, for instance). It took me quite a lot today to discover why a query was not producing the expected results.
The text was updated successfully, but these errors were encountered: