JdbcTemplate 'IN' search not working properly

白昼怎懂夜的黑 提交于 2019-12-11 16:25:02

问题


I am trying to perform IN search using JbdcTemplate in Spring. Here goes my code

@Override
    public Map<String, List> dataRetriveForAsset(String type) {

List<Integer> interfaceIdList = new ArrayList<Integer>();
List<Integer> fileList = new ArrayList<Integer>();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

String sql2 = "select interface_id from tbl_interface_asset where asset_id in ( :ids )";

//fileList is populated with a different query

Set<Integer> ids = new HashSet(Arrays.asList(new Integer[fileList.size()] ));

        for(int i=0; i<fileList.size();i++)
        {
            ids.add(fileList.get(i));
        }       
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("ids", ids);

interfaceIdList = jdbcTemplate.query(sql2,new ListMapper1(),parameters );

and the sql2 query part executes it throws the following error.

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/iccdashboard] threw exception [Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [select interface_id from tbl_interface_asset where asset_id in ( :ids )]; Invalid argument value: java.io.NotSerializableException; nested exception is java.sql.SQLException: Invalid argument value: java.io.NotSerializableException] with root cause
java.io.NotSerializableException: org.springframework.jdbc.core.namedparam.MapSqlParameterSource
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at com.mysql.jdbc.PreparedStatement.setSerializableObject(PreparedStatement.java:4401)
    at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:4083)
    at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:351)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)

But if I use NamedParameterJdbcTemplate, I am getting a BadSQLGrammarError because the 'ids' value my sql query goes emplty like below.

select interface_id from tbl_interface_asset where asset_id in ( )

回答1:


java.io.NotSerializableException: org.springframework.jdbc.core.namedparam.MapSqlParameterSource

I was able to fix this problem by replacing my usage of

org.springframework.jdbc.core.JdbcTemplate

with the:

org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate

I was using the MapSqlParameterSource class but using the standard template which may have been the problem.

But if I use NamedParameterJdbcTemplate, I am getting a BadSQLGrammarError because the 'ids' value my sql query goes empty like below.

Can you make the SQL generation conditional on whether or not there are entries in your IN collection?

if (!ids.isEmpty()) {
    parameters.addValue("ids", ids);
}


来源:https://stackoverflow.com/questions/20424989/jdbctemplate-in-search-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!