2015-04-27 187 views
23

我有一個簡單的存儲過程用於測試Spring Data JPA存儲過程功能。Spring Data JPA NamedStoredProcedureQuery多個輸出參數

create or replace procedure plus1inout (arg in int,res1 out int,res2 out int) is 
BEGIN 
res1 := arg + 1; 
res2 := res1 + 1; 
END; 

我的代碼是:

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

@Entity 
@NamedStoredProcedureQuery(name = "plus1", procedureName = "ADJUD.PLUS1INOUT", 
     parameters = { 
     @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class), 
     @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res1", type = Integer.class), 
     @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res2", type = Integer.class) 
}) 
public class AdjudConverDateSP implements Serializable { 
     //stub to satisfy hibernate identifier requirement 
     @Id @GeneratedValue 
     private Long id; 

} 

一切正常時,我有一個OUT參數。但是一旦我添加了第二個OUT參數,我就會得到一個異常,說它在實體中找不到該過程。

Caused by: 
    org.springframework.data.mapping.PropertyReferenceException: No property plus1 found for type AdjudConverDateSP! at 
    org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75) at 
    org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) at 
    org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) at 
    org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) at 
    org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241) at 
    org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) at 
    org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235) at 
    org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) at 
    org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353) 
+0

您使用哪種Spring Data JPA版本? –

+0

Spring-Data JPA 1.8.0版本 –

+0

這個https://github.com/spring-projects/spring-data-examples/issues/80在git倉庫中存在一個問題......當他們回答這個問題時去實現它。 –

回答

5

它看起來像@Procedure預計僅這是直接綁定到該方法返回一個類型OUT參數...

要處理多個OUT參數,你可以直接使用JPA的API:

StoredProcedureQuery proc = em.createNamedStoredProcedureQuery("plus1"); 

proc.setParameter("arg", 1); 
proc.execute(); 
Integer res1 = (Integer) proc.getOutputParameterValue("res1"); 
Integer res2 = (Integer) proc.getOutputParameterValue("res2"); 
... 
+0

最終使用JPA API就是我所做的。你在哪裏發現@Procedure只需要一個OUT參數? –

+1

API除了只有一個OUT參數http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/query/Procedure.html –

+0

能否請您提供完整的例子。我是新的,無法通過實體管理器調用命名存儲過程。 –