JPA 2.1 StoredProcedureQuery with PostgreSQL and REF_CURSORs

前端 未结 1 592
天命终不由人
天命终不由人 2021-01-13 03:50

I have a function I created in my PostgreSQL DB that I want to call using JPA 2.1\'s StoredProcedureQuery method.

Here is my PostgreSQL query:

CREA         


        
1条回答
  •  太阳男子
    2021-01-13 04:27

    Short answer: Reverse the order of your two calls to registerStoredProcedureParameter():

    storedProcedure.registerStoredProcedureParameter(1, Object.class, ParameterMode.REF_CURSOR);
    storedProcedure.registerStoredProcedureParameter(2, String.class, ParameterMode.IN);
    

    Long answer: I did some digging in the Hibernate source code for Postgress callable statement support, and found that each registerStoredProcedureParameter() call creates a ParameterRegistrationImplementor instance that gets tacked into a list and passed around. You'll note that this class stores the position of the parameter, which is independent of its position within the list.

    Later, this list is analyzed and assumes that the REF_CURSOR parameter will be first in line, and throws your error message if a REF_CURSOR parameter is not first, regardless of what the parameter number is.

    Not a very bright way of doing things (IMHO), but at least the workaround is easy: if you swap the order of your calls, you should be fine.

    0 讨论(0)
提交回复
热议问题