java Spring Data JPA NamedStoredProcedureQuery 多个输出参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29899219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA NamedStoredProcedureQuery Multiple Out Parameters
提问by hello_world_infinity
I have a simple stored procedure I'm using to test out Spring Data JPA Stored Procedure feature.
我有一个简单的存储过程,用于测试 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;
My code is:
我的代码是:
@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;
}
Everything works fine when I have a single OUT parameter. But once I add a second OUT parameter I get an exception saying it can't find the procedure in the entity.
当我只有一个 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)
回答by Julien
It looks like @Procedure
expects only one OUT parameter which is binded directly to the method return type...
看起来@Procedure
只需要一个直接绑定到方法返回类型的 OUT 参数......
To handle multiple OUT params you can use the JPA API directly:
要处理多个 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");
...
回答by Jeff Sheets
You can specify to return one of the multiple out params with the outputParameterName
param in the @Procedure
annotation like this:
您可以使用注释中的outputParameterName
参数指定返回多个输出参数之一,@Procedure
如下所示:
@Repository
public interface AdjudConverDateSPRepository extends JpaRepository<AdjudConverDateSP, Long> {
@Procedure(name = "plus1", outputParameterName = "res2")
Integer plus1(@Param("arg") Integer arg);
}
UPDATE 2019-06-24:
更新 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
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
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:
接口方法只需要有一个 Map 返回类型,以便可以通过键名访问每个输出参数:
@Repository
public interface AdjudConverDateSPRepository extends JpaRepository<AdjudConverDateSP, Long> {
@Procedure(name = "plus1")
Map<String, Object> plus1(@Param("arg") Integer arg);
}
回答by Tezuka
回答by Vikky
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.
Spring Data JPA 支持多个输出参数。Method 的返回类型必须是 Map。我花了很多时间在这上面。下面的链接正好给出了一个例子,搜索 User.plus1IO2。