Spring Data JPA NamedStoredProcedureQuery Multiple Out Parameters

前端 未结 4 1577
青春惊慌失措
青春惊慌失措 2021-02-01 04:04

I have a simple stored procedure I\'m using to test out Spring Data JPA Stored Procedure feature.

create or replace procedure plus1inout (arg in int,res1 out int         


        
相关标签:
4条回答
  • 2021-02-01 04:31

    Spring Data JPA support multiple output parameters. Return type of Method must be a Map. I spent a lot of time on this. Below link exactly gives example of that, Search for User.plus1IO2.

    User.java

    UserRepository.java

    0 讨论(0)
  • 2021-02-01 04:34

    Spring doesn't support multiple out params just yet. There is a JIRA for this.

    0 讨论(0)
  • 2021-02-01 04:39

    It looks like @Procedure expects only one OUT parameter which is binded directly to the method return type...

    To handle multiple OUT params you can use the JPA API directly:

    StoredProcedureQuery proc = em.createNamedStoredProcedureQuery("plus1");
    
    proc.setParameter("arg", 1);
    proc.execute();
    Integer res1 = (Integer) proc.getOutputParameterValue("res1");
    Integer res2 = (Integer) proc.getOutputParameterValue("res2");
    ...
    
    0 讨论(0)
  • 2021-02-01 04:55

    You can specify to return one of the multiple out params with the outputParameterName param in the @Procedure annotation like this:

    @Repository
    public interface AdjudConverDateSPRepository extends JpaRepository<AdjudConverDateSP, Long> {
        @Procedure(name = "plus1", outputParameterName = "res2")
        Integer plus1(@Param("arg") Integer arg);
    }
    

    UPDATE 2019-06-24:

    Multiple out parameters is now supported in Spring Data JPA 2.2-RC1 https://spring.io/blog/2019/06/17/spring-data-moore-rc1-and-lovelace-sr9-released

    https://jira.spring.io/browse/DATAJPA-707

    The interface method just needs to have a Map return type so each out param can be accessed by key name:

    @Repository
    public interface AdjudConverDateSPRepository extends JpaRepository<AdjudConverDateSP, Long> {
        @Procedure(name = "plus1")
        Map<String, Object> plus1(@Param("arg") Integer arg);
    }
    
    0 讨论(0)
提交回复
热议问题