Search code examples
javaspringjpastored-proceduresentitymanager

"SQLException: Missing IN or OUT parameter" despite being explicitly registered


I have a spring boot application using JPA with a custom implementation to call a stored procedure. The problem is, I get "Missing IN or OUT parameter at index:: 7" when executing the call.

The implementation looks like this:

public class DbLogImpl implements DbLogCustom {
    @PersistenceContext
    private EntityManager em;

    @Override
    public Long callInsertLog(Long firstId, Long secondId, Long process, Long subProcess, Long logLevelId, String message, String stacktrace) {

        Long logId = null;

        StoredProcedureQuery proc = em.createStoredProcedureQuery("logging_pk.insert_log");
        proc.registerStoredProcedureParameter(0, Long.class, IN);
        proc.registerStoredProcedureParameter(1, Long.class, IN);
        proc.registerStoredProcedureParameter(2, Long.class, IN);
        proc.registerStoredProcedureParameter(3, Long.class, IN);
        proc.registerStoredProcedureParameter(4, Long.class, IN);
        proc.registerStoredProcedureParameter(5, String.class, IN);
        proc.registerStoredProcedureParameter(6, String.class, IN);
        proc.setParameter(0, firstId);
        proc.setParameter(1, secondId);
        proc.setParameter(2, process);
        proc.setParameter(3, subProcess);
        proc.setParameter(4, logLevelId);
        proc.setParameter(5, message);
        proc.setParameter(6, stacktrace);
        proc.registerStoredProcedureParameter(7, Long.class, OUT);
        proc.execute();

        logId = (Long) proc.getOutputParameterValue(7);

        return logId;
    }
}

My package looks like this:

CREATE OR REPLACE PACKAGE logging_pk AS

    PROCEDURE insert_log (
        i_first_id                      NUMERIC,
        i_second_id                     NUMERIC,
        i_process                       NUMERIC,
        i_sub_process                   NUMERIC,
        i_log_level_id                  NUMERIC,
        i_message                       VARCHAR2,
        i_stacktrace                    VARCHAR2,
        o_log_id                    OUT NUMERIC
    );

END;

If this was my only procedure I might very well have admitted defeat and tried another approach. (I have managed to get it working with a native query, though without actually getting the output parameter value.)

However, I have another stored procedure call which is setup exactly like this one, and which works fine. The only difference would be the names and the number of parameters (the working one has 3 input and 1 output, also with the output last).

I cannot understand why one is working fine while the other is not.

EDIT: As I found the problem before posting this question, I decided to omit a lot of code that is irrelevant to the question, but I will leave this here for others to find.


Solution

  • While writing this question I suddenly had an idea.

    Turns out, the solution was rather simple.

    Apparently, setting a String parameter to null is not accepted. By introducing null checks and setting null strings (the stacktrace parameter in this case) to empty string instead solved the problem.

    Leaving this here in case someone else runs into this problem.