带有 SimpleJdbcCall 的 spring ArrayIndexOutOfBoundsException [英] spring ArrayIndexOutOfBoundsException with SimpleJdbcCall

查看:36
本文介绍了带有 SimpleJdbcCall 的 spring ArrayIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要调用的 Oracle 过程:

This is the Oracle procedure I'm trying to call:

 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 代码:

Here's my Java code:

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() 时,出现以下异常:

When getReportsList() calls execute(), I get the following Exception:

java.lang.ArrayIndexOutOfBoundsException: 2org.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)

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)

任何关于我做错了什么的提示?

Any hints as to what I'm doing wrong?

推荐答案

问题在这里

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

您正在调用 重载 方法,它接受一个 Object ....这个方法需要

You are calling the overloaded method that takes in a Object .... This method takes

包含要在打电话.参数值的提供顺序必须与参数是为存储过程定义的.

optional array containing the in parameter values to be used in the call. Parameter values must be provided in the same order as the parameters are defined for the stored procedure.

在您的调用中,这两个参数恰好是一个空的 HashMap 和一个 SqlParameterSource.

In your call, those two parameters happen to be an empty HashMap and a SqlParameterSource.

CallMetaDataContext#matchInParameterValuesWithCallParameters() 中的调用失败.来源:

The call fails in CallMetaDataContext#matchInParameterValuesWithCallParameters(). Source:

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.当它尝试访问第三个(索引 2)时,它会抛出一个 ArrayIndexOutOfBoundsException.

It's expecting that the array you pass has 7 elements (because that's what your stored procedure expects), but it only has 2, the HashMap and the SqlParameterSource. When it tries to access the 3rd one (index 2), it throws an ArrayIndexOutOfBoundsException.

您可能想使用 execute() 接受 SqlParameterSource

You probably wanted to use the execute() method that accepts an SqlParameterSource

Map m = procGetReportExtras.execute(in);

这篇关于带有 SimpleJdbcCall 的 spring ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆