问题
I try the following code:
ProcedureCall call = ss.createStoredProcedureCall("servicos.CLASSIFIER_MAP");
call.registerParameter( 1, String.class, ParameterMode.IN).bindValue(classifierString);
call.registerParameter( 2, Long.class, ParameterMode.IN ).bindValue(totalRows);
Sometimes the variable totalRows is null and an error appears
WARN: SQL Error: 17041, SQLState: 99999
Error calling CallableStatement.getMoreResults
It is possible to send null values into a ProcedureCall in hibernate?
回答1:
You may try calling your stored procedure via CallableStatement instead of ProcedureCall.
And try and leverage its setNull method to set procedure IN parameters as NULL.
But for your answer whether hibernate supports passing NULL value, it seem to support default parameter values over substituting a null value.
回答2:
Currently there are 2 ways to achieve your objective :
- Set the property
param_null_passingto true in your hibernate configuration.<property name="hibernate.proc.param_null_passing">true</property>
This is a global setting which will enable you to pass null values in toProcedureCallthroughout your application. If you do not wish to apply the above setting globally, change your code to the following :
ProcedureCall call = ss.createStoredProcedureCall("servicos.CLASSIFIER_MAP"); call.registerParameter( 1, String.class, ParameterMode.IN).bindValue(classifierString); call.registerParameter( 2, Long.class, ParameterMode.IN ).enablePassingNulls(true); call.setParameter(2, totalRows);
From the Hibernate 5 documentation :
hibernate.proc.param_null_passing (default value is false) :
Global setting for whether null parameter bindings should be passed to database procedure/function calls as part of ProcedureCall handling. Implicitly Hibernate will not pass the null, the intention being to allow any default argument values to be applied.
This defines a global setting, which can then be controlled per parameter via org.hibernate.procedure.ParameterRegistration#enablePassingNulls(boolean)
Values are true (pass the NULLs) or false (do not pass the NULLs).
来源:https://stackoverflow.com/questions/39922446/null-values-in-procedurecall