Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Spring jdbctemplate to invoke a oracle function returning cursor

Hi I am using spring jdbctemplate to invoke an oracle function which takes an integer input.

Oracle function is :
    FUNCTION get_key_types (type_id IN integer)
  RETURN tcur_key_types_det
IS 
  lv_cur   tcur_key_types_det;
 BEGIN
  IF type_id IS NULL
  THEN
     RAISE KEY_ERROR;
  ELSE
     OPEN lv_cur FOR
          SELECT   key_criteria_cd,
                   key_type_name,
                   KEY_COLUMN_TXT,
                   data_type_cd
            FROM   key_criteria
           WHERE   criteria_type_id = type_id
        ORDER BY   key_type_name;
  END IF;
RETURN lv_cur;
END get_key_types;

I have a Java class that extends from Class StoredProcedure which passes and integer argument to invoke the oracle function as follows.

public class KeyTypeService extends StoredProcedure{
public KeyTypeService(DataSource dataSource,String sqlString) {
    setDataSource(dataSource);
    setFunction(true);
    setSql(sqlString);
    declareParameter(new SqlParameter("type_id",Types.INTEGER));
    declareParameter(new SqlOutParameter("functionName",OracleTypes.CURSOR,new KeyTypeMapper()));
    compile();
}

public Map execute(int category_id) {
    Map<String, Object> inputs = new HashMap<String, Object>();
    inputs.put("type_id", category_id);
    Map output = execute(inputs);
    return output;     
}

}

I invoke the oracle function as follows.

int i = 60;
KeyTypeService keyTypeService = new KeyTypeService((DataSource)c.getBean   ("DataSource"),"get_key_types"); 
map = keyTypeService.execute(i);

I get the following error indicating that I am not passing the correct data type expected.

PLS-00306: wrong number or types of arguments in call to 'get_key_types' ORA-06550: line 1, column 7: PL/SQL: Statement ignored

Any help would be highly appreciated..

like image 662
urug Avatar asked Aug 09 '26 19:08

urug


1 Answers

Since my comment was correct:

The out parameter must come first.

Spring documentation might not explicitly talk about the order but the parameter order must be the same order as when you would call a pl/sql function without spring. I.e.:

out = call pacakge.function(in)
like image 63
tom Avatar answered Aug 11 '26 11:08

tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!