使用SimpleJdbcCall弹出ArrayIndexOutOfBoundsException

时间:2013-08-30 16:29:55

标签: spring oracle stored-procedures spring-jdbc

这是我试图调用的Oracle过程:

 PROCEDURE GetCoreReportExtras
( pnAssignment IN NUMBER,
  pnUserRole in NUMBER,
  psAreaMenu in VARCHAR2,
  pnAreaLevel in NUMBER,
  curReportList OUT outcur,
  psLDO in VARCHAR2 default 'none',
  pnAcisNumber in NUMBER default 0);

这是我的Java代码:

private class GetStandardReportExtrasSPV2{
    int nAreaLevel;
    int nAssignment;
    int nUserRole;
    int nAcisNum = 0;
    String strAreaMenu;     
    String strLDO = null;

    private SimpleJdbcTemplate simpleJdbcTemplate;
    private SimpleJdbcCall procGetReportExtras;

    public GetStandardReportExtrasSPV2(DataSource ds, int nUserRole, String strAreaMenu,
            int nAssignment, int nAreaLevel, String strLDO, int nAcisNum) {

        this.simpleJdbcTemplate = new SimpleJdbcTemplate(ds);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        jdbcTemplate.setResultsMapCaseInsensitive(true);
        this.procGetReportExtras =
                new SimpleJdbcCall(jdbcTemplate)
                    .withCatalogName("package")
                    .withProcedureName("proc")
                    .returningResultSet(REPORT_LIST,
                                ParameterizedBeanPropertyRowMapper.newInstance(Report.class));

    }

    public List<Report> getReportsList() {

        Map<String, Object> params = new HashMap<String, Object>();

        params.put(USER_ASSIGNMENT, nAssignment);
        params.put(USER_ROLE, nUserRole);
        params.put(AREA_MENU, strAreaMenu);
        params.put(AREA_LEVEL, nAreaLevel);
        params.put(SEGMENT, strLDO);
        params.put(ACIS_NUMBER, nAcisNum);

        SqlParameterSource in = new MapSqlParameterSource()
        .addValues(params);

        Map m = procGetReportExtras.execute(new HashMap<String, Object>(0), in);
        return (List) m.get(REPORT_LIST);
    }

}

当getReportsList()调用execute()时,我得到以下异常:

  

java.lang.ArrayIndexOutOfBoundsException:2     org.springframework.jdbc.core.metadata.CallMetaDataContext.matchInParameterValuesWithCallParameters(CallMetaDataContext.java:555)     org.springframework.jdbc.core.simple.AbstractJdbcCall.matchInParameterValuesWithCallParameters(AbstractJdbcCall.java:419)     org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:364)     org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:173)

关于我做错了什么提示?

2 个答案:

答案 0 :(得分:1)

问题在这里

Map m = procGetReportExtras.execute(new HashMap<String, Object>(0), in);

您正在调用Object ...的{​​{3}}方法。

采用此方法
  

可选数组,包含要在其中使用的in参数值   呼叫。参数值的提供顺序必须与   为存储过程定义参数。

在您的通话中,这两个参数恰好是空的HashMapSqlParameterSource

CallMetaDataContext#matchInParameterValuesWithCallParameters()中的通话失败。来源:

public Map<String, ?> matchInParameterValuesWithCallParameters(Object[] parameterValues) {
    Map<String, Object> matchedParameters = new HashMap<String, Object>(parameterValues.length);
    int i = 0;
    for (SqlParameter parameter : this.callParameters) {
        if (parameter.isInputValueProvided()) {
            String parameterName =  parameter.getName();
            matchedParameters.put(parameterName, parameterValues[i++]); // fails here
        }
    }
    return matchedParameters;
}

期望您传递的数组有7个元素(因为这是您的存储过程所期望的),但它只有2个,HashMapSqlParameterSource。当它尝试访问第3个(索引2)时,它会抛出ArrayIndexOutOfBoundsException

您可能想要使用overloaded

Map m = procGetReportExtras.execute(in);

答案 1 :(得分:0)

你说错了SimpleJDBCCall.execute(Object... args);使用SimpleJDBCCall.execute(SqlParameterSource)

procGetReportExtras.execute(in);