0

I have a store procedure in database which contains multiple OUT parameters. I want to use JPA (with Hibernate) to get the resultset. Is there any way to get all results from the OUT parameters?

E.g. When using CallableStatement you can use ".registerOutParameter" for those parameters. I want the equivalent for JPA (if exists/possible).

Database used is PostgreSQL.

4

3 回答 3

1

JPA2.1 将根据您的需要提供 StoredProcedure 支持(尚未最终确定)。提供此 AFAIK 预览功能的唯一 JPA 实现是DataNucleus JPA。其他 JPA impls 可能有自己的非标准支持,所以如果想走这条路,请查看您的手册

于 2012-04-20T11:39:47.800 回答
0

在这里,您可以通过实现自定义存储库轻松地从 Spring Data JPA 存储库调用存储过程。

  1. 创建一个接口并定义您的方法。
  2. 为它创建 impl 使用 @Repository 进行注释
  3. 下面的方法实现下是ti实现的方式

     StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("Your Stored Procedure name");
    
    // Set the parameters of the stored procedure.
    String firstParam = "first param name";
    storedProcedure.registerStoredProcedureParameter(firstParam, Long.class, ParameterMode.IN);
    storedProcedure.setParameter(firstParam, first Param value);
    
    // Set the second parameters of the stored procedure.
    String secondParam = "second parameter name";
    storedProcedure.registerStoredProcedureParameter(secondParam, String.class, ParameterMode.IN);
    storedProcedure.setParameter(secondParam, "second paramter value);
    
    //Your OUT parameters.
    storedProcedure.registerStoredProcedureParameter("First OUT parameter name", Integer.class, ParameterMode.OUT);
    storedProcedure.registerStoredProcedureParameter("OUT second parameter name", Integer.class, ParameterMode.OUT);
    storedProcedure.execute();
    //Replace MyObject with your actual POJO
    MyObject myObject= new MyObject();
    myObject.setFirstParameterOutput((Integer) storedProcedure.getOutputParameterValue("first OUT parameter name"));
    myObject.setSecondParameterOutput((Integer) storedProcedure.getOutputParameterValue("second OUT parameter name"));
    return MyObject;
    

注意:即使您可以通过注释 @Procedure 从存储库本身调用也不行

@Procedure
public Integer procedure_name(String firstParameter, Date secondParameter);

 }
于 2017-09-19T13:56:09.130 回答
0

这个解释是基于 Spring Boot + Spring Data JPA + Hibernate。

  1. 在实体类中@NamedStoredProcedureQuery为您的存储过程定义一个,如下所示。Example在此示例中,假定存储过程具有IN,INOUTOUTtype 参数。

    @NamedStoredProcedureQueries({
        @NamedStoredProcedureQuery(
            name = "Example.myStoredProcedure",
            procedureName = "db.MY_STORED_PRODEDURE",
            parameters = {
                @StoredProcedureParameter(mode = ParameterMode.IN, name = "pInParam", type = String.class),
                @StoredProcedureParameter(mode = ParameterMode.OUT, name = "pOutParam", type = Date.class),
                @StoredProcedureParameter(mode = ParameterMode.INOUT, name = "pInOutParam", type = Integer.class),
            }
        )
    })
    @Entity
    public class Example {
        @Id
        Integer id;
    
        ...
    }
    
  2. 在类中定义一个方法,ExampleRepository只使用存储过程的ININOUT参数。正确设置参数procedureNamevalue的值@Procedure。此方法的返回类型应始终为Map<String, ?>.

    @Repository
    public interface ExampleRepository extends JpaRepository<Example, Integer> {
           @Procedure(value = "Example.myStoredProcedure", procedureName = "db.MY_STORED_PRODEDURE")
            Map<String, ?> executeMyStoredProcedure(@Param("pInParam") String pInParam, @Param("pInOutParam") String pInOutParam);
    
    }
    
  3. 在类中调用上面定义的方法,ExampleService如下所示。您现在可以使用步骤 1 中定义的名称从方法返回的值中检索多个OUT值(INOUTs 太) 。Map<String, ?>@StoredProcedureParameter

    @Service
    public Class ExampleService {
       final ExampleRepository exampleRepository;
    
       public ExampleService(ExampleRepository exampleRepository) {
           this.exampleRepository = exampleRepository;
       }
    
       public void executeMyStoredProcedure(String someString, Integer someInteger) {
           Map<String, ?> result = exampleRepository.executeMyStoredProcedure(someString, someInteger);
    
           Date dateValue = (Date) result.get("pOutParam");
           Integer integerValue = (Integer) result.get("pInOutParam");
    
           ...
    
       }
    }
    
    
于 2020-05-24T00:10:21.917 回答