Using “where in” in spring-jdbc

2019-08-11 15:15发布

问题:

Is there a way to delete a bunch of elements using "where... in" in SQL, like so:

HashSet<String> idStrings = ...;
SimpleJdbcTemplate template = getTemplate();
template.update("DELETE FROM records WHERE idstring IN (?)", idStrings);

I am trying to get some old code to work that uses this method, but whenever I try to run it the Oracle JDBC drivers throw an exception:

QL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type

This is with ojdbc5 11.2.0.1.0 and spring-jdbc 3.0.3

回答1:

It's possible, but you need one placeholder (?) per ID, and each ID must be bound separately.

You could also use NamedParameterJdbcTemplate, which

[...] also allows for expanding a List of values to the appropriate number of placeholders.

Be careful not to put too many values in the set of IDs. Oracle limits them to 1000, for example.