Is it possible to pass a null parameter to a stored procedure in Java JPA 2.1?

前端 未结 13 1782
孤城傲影
孤城傲影 2020-12-08 15:07

Using the new JPA 2.1 stored procedure call, is there any way to pass a null parameter?

Here is an example usage:

StoredProcedureQuery storedProcedur         


        
13条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 15:46

    Yes, it is possible to pass null params to stored procedures when using JPA StoredProcedureQuery.

    You have to add the following property in application.properties file and register the parameters with their name.

    spring.jpa.properties.hibernate.proc.param_null_passing=true

    Example:

    StoredProcedureQuery q = em.createStoredProcedureQuery(Globals.SPROC_PROBLEM_COMMENT2, ProblemCommentVO.class);
    q.registerStoredProcedureParameter("Patient_ID", Long.class, ParameterMode.IN);
    q.registerStoredProcedureParameter("Param2", Long.class, ParameterMode.IN);
    q.registerStoredProcedureParameter("Param3", Long.class, ParameterMode.IN);
    q.registerStoredProcedureParameter("Param4", Integer.class, ParameterMode.OUT);
    q.setParameter("Patient_ID", patientId);
    q.setParameter("Param2", null);//passing null value to Param2
    q.setParameter("Param3", null);
    
    List pComments = q.getResultList();
    Integer a = (Integer) q.getOutputParameterValue("Param4");
    

提交回复
热议问题