Skip to content

Iterable argument for NamedParameterJdbcTemplate not correctly processed #26467

Closed
@Serranya

Description

@Serranya

Affects: spring-jdbc 5.3.3


We have the following code:

 return jdbcTemplate.query("SELECT whatever FROM table WHERE id IN (:ids)",
                new MapSqlParameterSource("ids",
                        new SqlParameterValue(BIGINT, orders.stream().map(Order::getOrderId)
                                                            .collect(Collectors.toSet()))),
                rs -> {
                   // elided...
                });

... Spring correctly identifies the parameter value as Iterable and replaces the :ids with the correct number of question marks.

But here

if (in instanceof Iterable && declaredParameter.getSqlType() != Types.ARRAY) {

Spring does not correctly identify the value as an Iterable. This is because a few lines earlier

if (in instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) in;
in = paramValue.getValue();
declaredParameter = paramValue;
}

... Spring unwraps the SqlParameterValue, but its content is just another SqlParameterValue which contains the actual Iterable.

We can use the following code to circumvent the problem:

 return jdbcTemplate.query("SELECT whatever FROM table WHERE id IN (:ids)",
                new MapSqlParameterSource()
                    .addValue("ids", orders.stream().map(Order::getOrderId)
                                                            .collect(Collectors.toSet())),
                rs -> {
                   // elided...
                });

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: dataIssues in data modules (jdbc, orm, oxm, tx)status: feedback-providedFeedback has been providedstatus: supersededAn issue that has been superseded by anothertype: regressionA bug that is also a regression

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions