1

I have looked at a number of StackOverflow questions on this issue but can't find one that makes any sense. This one comes closest but doesn't show how to get the return value from the function.

Here's my mapper call:

public Long callMyFunction(@Param("recordId") Long recordId, @Param("otherId") Long otherId, @Param("date") Date date, @Param("comments") String comments);

Here's the mapper XML:

<select id="callMyFunction" parameterType="map" statementType="CALLABLE"  resultType="java.lang.Long">
    {  #{resultId,javaType=java.lang.Long,jdbcType=NUMERIC,mode=OUT} = call MYSCHEMA.MYPACKAGE.my_function(
    #{recordId,jdbcType=NUMERIC,mode=IN},
    #{otherId,jdbcType=NUMERIC,mode=IN},
    #{date,jdbcType=DATE,mode=IN},
    #{comments,jdbcType=VARCHAR,mode=IN})}
</select>

The call works, but the return value (resultId) is always null.

Can anybody spot the problem?

1
  • Have you tried it with recordId, javaType=long (native type) rather than Long (object wrapper)? Commented Dec 21, 2016 at 1:02

1 Answer 1

3

If you want to return directly the result, then the SQL call must be: SELECT MYSCHEMA.MYPACKAGE.my_function(...) FROM DUAL

If you want to keep with calling the function in the procedure call style, that means the result is an OUT parameter (you env declared it OUT). The minimum change would consist in adding a parameter to the mapper method signature: public Long callMyFunction(@Param("recordId") Long recordId, @Param("otherId") Long otherId, @Param("date") Date date, @Param("comments") String comments, @Param("resultIdContainer") Map<String, Long> resultIdContainer);

In the XML: forget the resultType, this is for selects. And the call: { #{resultIdContainer.resultId, javaType=java.lang.Long,jdbcType=NUMERIC,mode=OUT} = call ...

Not that I use here a map to contain the resutlId: an indirection is required: the function will write the parameter 'result' value somewhere you can read later (after your mapper call), you can also use a class with a resultId property.

Sign up to request clarification or add additional context in comments.

5 Comments

That was precisely what I needed, thank you very much!
in the first variant ORA-00923: FROM keyword not found where expected
Fixed. It worked as it was in other DBMS, while in Oracle we must indeed select anything FROM DUAL
What if my jdbcType is Cursor?
Should be call #{params.resultList, jdbcType=CURSOR, javaType=java.sql.ResultSet, resultMap=myResultMap, mode=OUT} = my_function() which is same principle as with procedure: call my_procedure(#{params.resultList, jdbcType=CURSOR, javaType=java.sql.ResultSet, resultMap=myResultMap, mode=OUT}). Note that params is used for input AND output. The mapper result type is void.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.