我正在尝试使用org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate批量更新表。 namedParameterJdbcTemplate.batchUpdate(query ...)方法; 我的查询包含子句,因此我收到如下SQL错误:
nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc][1091][10824][4.14.113] Invalid data conversion: Parameter instance [1271, 1272] is invalid for the requested conversion. ERRORCODE=-4461, SQLSTATE=42815.
下面是我要批量更新的数据。它是contractId(String)的映射作为键,值是taskids列表(整数) 数据:
{10008=[1271, 1272], 10007=[1269, 1270], 10009=[1273, 1274], 10011=[1277, 1278], 10010=[1275, 1276], 10012=[1279], 10006=[1267, 1268]}
where key is the CONTRACT_ID (String) and value is list of TASK_ID ( Integer)
e.g. CONTRACT_ID are 10008, 10007, 10009 etc.
and respective TASK_ID are [1271, 1272], [1269, 1270], [1273, 1274] etc.
我尝试在循环内调用namedParameterJdbcTemplate.update,并且工作正常,我可以使用单个更新来更新同一查询
@Override
public int assignUserTask(String customerId, String userId, Map<String, List<Integer>> resultMap)
throws ApplicationException {
int totalCount = 0;
String ts = DateUtils.getCurrentDatetimeStamp();
String todaysDate = DateUtils.getTodaysDate();
String query = CommonUtils.buildQuery("UPDATE EM_WF_USER_QUEUE SET", "CURRENT_USER_ID = :currentUserId,",
"INITIAL_USER_ID = :initialUserId,", "USER_REASSIGN_TYPE_CD = 'GW',",
"USER_REASSIGN_TIME = :userReassignTime,", "LAST_CENTRAL_QUEUE_DATE = :lastCentralQueueData,",
"REASSIGN_CENTRAL_DATE = :reassignCentralDate,", "ORG_GW_CRT_TIME = :orgGwCreateTime,",
"LAST_UPDT_USER = :lastUpdtUser ,", "LAST_UPDT_TIME = :lastUpdtTime",
"WHERE CONTRACT_ID = :contractId AND TASK_ID IN (:taskIds)",
"AND CUSTOMER_ID = :customerId AND CURRENT_USER_ID = 'UNASSIGNED'",
"AND OVERIDE_IND = 'N' AND QUEUE_STATUS != 'CLOSED'");
try {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("currentUserId", userId);
params.addValue("initialUserId", userId);
params.addValue("userReassignTime", ts);
params.addValue("lastCentralQueueData", todaysDate);
params.addValue("reassignCentralDate", todaysDate);
params.addValue("orgGwCreateTime", ts);
params.addValue("lastUpdtUser", userId);
params.addValue("lastUpdtTime", ts);
params.addValue("customerId", customerId);
for (Entry<String, List<Integer>> entry : resultMap.entrySet()) {
params.addValue("contractId", entry.getKey());
params.addValue("taskIds", entry.getValue());
totalCount += namedParameterJdbcTemplate.update(query, params);
}
return totalCount;
} catch (DataAccessException exp) {
throw new CustomException(exp, "Exception occured!");
}
}
@Override 公共诠释assignUserTask(字符串customerId,字符串userId,地图> resultMap) 引发CustomException {
String ts = DateUtils.getCurrentDatetimeStamp();
String todaysDate = DateUtils.getTodaysDate();
String query = CommonUtils.buildQuery("UPDATE EM_WF_USER_TASK SET", "CURRENT_USER_ID = :currentUserId,",
"INITIAL_USER_ID = :initialUserId,", "USER_REASSIGN_TIME = :userReassignTime,",
"LAST_CENTRAL_QUEUE_DATE = :lastCentralQueueData,", "REASSIGN_CENTRAL_DATE = :reassignCentralDate,",
"ORG_GW_CRT_TIME = :orgGwCreateTime,", "LAST_UPDT_USER = :lastUpdtUser ,",
"LAST_UPDT_TIME = :lastUpdtTime", "WHERE CONTRACT_ID = :contractId AND TASK_ID IN (:taskIds)",
"AND CUSTOMER_ID = :customerId AND CURRENT_USER_ID = 'UNASSIGNED'",
"AND ACTIVE_IND = 'Y' AND TASK_STATUS != 'CLOSED'");
try {
List<MapSqlParameterSource> batchArgs = new ArrayList<>();
for (Entry<String, List<Integer>> entry : resultMap.entrySet()) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("currentUserId", userId);
params.addValue("initialUserId", userId);
params.addValue("userReassignTime", ts);
params.addValue("lastCentralQueueData", todaysDate);
params.addValue("reassignCentralDate", todaysDate);
params.addValue("orgGwCreateTime", ts);
params.addValue("lastUpdtUser", userId);
params.addValue("lastUpdtTime", ts);
params.addValue("contractId", entry.getKey());
params.addValue("taskIds", entry.getValue());
params.addValue("customerId", customerId);
batchArgs.add(params);
}
int[] retVal = namedParameterJdbcTemplate.batchUpdate(query,
batchArgs.toArray(new MapSqlParameterSource[resultMap.size()]));
return retVal.length;
} catch (DataAccessException exp) {
throw new CustomException(exp, "Exception occured!");
}
}
我期望在使用namedParameterJdbcTemplate.batchUpdate(query..)
时查询能够批量运行
但这会导致类型转换错误。
原因:org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;错误的SQL语法[UPDATE EM_WF_USER_TASK SET CURRENT_USER_ID =?,INITIAL_USER_ID =?,USER_REASSIGN_TYPE_CD = 'GW',USER_REASSIGN_TIME =?,LAST_CENTRAL_QUEUE_DATE =?, REASSIGN_CENTRAL_DATE =?,ORG_GW_CRT_TIME =?,LAST_UPDT_USER =? , LAST_UPDT_TIME =?哪里CONTRACT_ID =? AND TASK_ID IN(?,?)和 CUSTOMER_ID =? AND CURRENT_USER_ID ='UNASSIGNED'AND ACTIVE_IND = 'Y'和TASK_STATUS!='CLOSED'];嵌套异常为 com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc] [1091] [10824] [4.14.113]无效的数据转换:参数 实例[1271,1272]对于请求的转换无效。 错误代码= -4461,SQLSTATE = 42815 在org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:93) 在org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) 在org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) 在org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1402) 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:620) 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:634) 在org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:924) 在org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(NamedParameterBatchUpdateUtils.java:43)处 在org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(NamedParameterJdbcTemplate.java:357) 在com.medicare.mss.daoImpl.ContractDAO.assignUserTask(ContractDAO.java:274) ...还有164个 引起原因:com.ibm.db2.jcc.am.SqlSyntaxErrorException:[jcc] [1091] [10824] [4.14.113]无效的数据转换:参数 实例[1271,1272]对于请求的转换无效。 错误代码= -4461,SQLSTATE = 42815 在com.ibm.db2.jcc.am.ed.a(ed.java:677) 在com.ibm.db2.jcc.am.ed.a(ed.java:60) 在com.ibm.db2.jcc.am.ed.a(ed.java:103) 在com.ibm.db2.jcc.am.po.c(po.java:2630) 在com.ibm.db2.jcc.am.po.setObject(po.java:2409) 在com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setObject(HikariProxyPreparedStatement.java) 在org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:411) 在org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:232) 在org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:163) 在org.springframework.jdbc.core.BatchUpdateUtils.setStatementParameters(BatchUpdateUtils.java:70) 在org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils.access $ 000(NamedParameterBatchUpdateUtils.java:33) 在org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils $ 1.setValues(NamedParameterBatchUpdateUtils.java:50) 在org.springframework.jdbc.core.JdbcTemplate.lambda $ batchUpdate $ 2(JdbcTemplate.java:932) 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:605) ...另外169个 com.medicare.mss.exception.ApplicationException:assignUserTask发生错误! 在com.medicare.mss.daoImpl.ContractDAO.assignUserTask(ContractDAO.java:279) 在com.medicare.mss.daoImpl.ContractDAO $$ FastClassBySpringCGLIB $$ 30e66146.invoke() ... 在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
我看到问题出在batchUpdate查询中的IN子句中。请检查以下链接。希望这可以帮助: https://github.com/spring-projects/spring-framework/issues/21935