Spring Data JPA NamedStoredProcedureQuery多个输出参数

时间:2015-04-27 14:59:43

标签: java hibernate stored-procedures spring-data spring-data-jpa

我有一个简单的存储过程,用于测试Spring Data JPA存储过程功能。

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)

5 个答案:

答案 0 :(得分: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");
...

答案 1 :(得分:2)

您可以指定返回带有outputParameterName注释中的@Procedure参数的多个out参数之一,如下所示:

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

但是不幸的是,如其他评论中所述,返回所有参数尚未实现

答案 2 :(得分:1)

Spring尚不支持多种out参数。有一个JIRA

答案 3 :(得分:0)

Spring Data JPA支持多个输出参数。方法的返回类型必须为Map。我花了很多时间。下面的链接准确地给出了示例,搜索User.plus1IO2。

User.java

UserRepository.java

答案 4 :(得分:0)

大家好,基于 Jeff Sheets anwer 解决了我的问题,所以我也想帮忙

这是我的解决方案

CREATE PROCEDURE `cardById`(IN id int, out cardNumber varchar(16), out personId bigint)
BEGIN
    SELECT card_number, person_id into cardNumber, personId 
    FROM cards 
    WHERE card_number = id
    LIMIT 1;
END

在实体类中

@NamedStoredProcedureQuery(name = "Card.cardById",
    procedureName = "cardById", parameters = {
    @StoredProcedureParameter(mode = ParameterMode.IN, name = "id", type = String.class),
    @StoredProcedureParameter(mode = ParameterMode.OUT, name = "cardNumber", type = String.class),
    @StoredProcedureParameter(mode = ParameterMode.OUT, name = "personId", type = Long.class)

在仓库中

@Procedure(name = "Card.cardById")
Map<String, Object> cardById(String id);

快乐编码!