Skip to content

NamedParameterJdbcTemplate: treat Iterable parameters like Collections #22981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mauromol opened this issue May 16, 2019 · 2 comments
Closed
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@mauromol
Copy link

mauromol commented May 16, 2019

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 a Collection.
Example:

    Map<String, ?> params = new HashMap<>();
    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 Iterable 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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 16, 2019
@jhoeller jhoeller self-assigned this May 16, 2019
@jhoeller jhoeller added in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 16, 2019
@jhoeller jhoeller added this to the 5.2 M3 milestone May 16, 2019
@jhoeller
Copy link
Contributor

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 jhoeller reopened this Jun 12, 2019
@jhoeller jhoeller changed the title NamedParameterJdbcTemplate: treat parameter arrays and Iterables like Collections NamedParameterJdbcTemplate: treat Iterable parameters like Collections Jun 12, 2019
@mauromol
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants