Spring JPA在存储过程中使用多个输出参数 – 异常”找不到类型的属性”

huangapple go评论165阅读模式
英文:

Spring Jpa Multiple output paramters in stored procedure - Exception "No property found for type"

问题

我使用spring-data-jpa:2.1.9.RELEASE(Spring boot 2.1.6 RELEASE)调用Oracle存储过程。

存储过程:

procedure proc(data in varchar2, res1 out number, res2 out number)
begin
  insert into table (id, data) values (abc.hibernate_sequence.nextval, data)
  returning id into res1;
  res2 := id*5;
end proc;

实体类:

@Entity
@Table(name = "table", schema = "abc")
@NamedStoredProcedureQuery(name = "testName", procedureName = "proc", parameters = {
    @StoredProcedureParameter(mode = ParameterMode.IN, name = "data", type = String.class),
    @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res1", type = Long.class),
    @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res2", type = Long.class) 
})
public class TestEntity { ... }

仓库类:

@Repository
public interface TestEntityRepository extends JpaRepository<TestEntity, Long> {

  @Transactional
  @Procedure(name = "testName")
  Map<String, Long> procedure(@Param("data") String data);
}

当我启动应用程序时:

IllegalArgumentException: Failed to create query for method public abstract java.util.Map TestEntityRepository.procedure(java.lang.String)! 
No property procedure found for type TestEntity

如果我只使用一个输出参数的存储过程,一切正常。

英文:

I call oracle stored procedure using spring-data-jpa:2.1.9.RELEASE (Spring boot 2.1.6 RELEASE)

Procedure:

procedure proc(data in varchar2, res1 out number, res2 out number)
begin
  insert into table (id, data) values (abc.hibernate_sequence.nextval, data)
  returning id into res1;
  res2 := id*5;
end proc;

Entity

@Entity
@Table(name = &quot;table&quot;, schema = &quot;abc&#39;)
@NamedStoredProcedureQuery(name = &quot;testName&quot;, procedureName = &quot;proc&quot;, parameters = {
    @StoredProcedureParameter(mode = ParameterMode.IN, name = &quot;data&quot;, type = String.class),
    @StoredProcedureParameter(mode = ParameterMode.OUT, name = &quot;res1&quot;, type = Long.class),
    @StoredProcedureParameter(mode = ParameterMode.OUT, name = &quot;res2&quot;, type = Long.class) 
})
public class testEntity { ... }

Repository

@Repository
public interface TestEntityRepository extends JpaRepository&lt;TestEntity, Long&gt; {

  @Transactional
  @Procedure(name = &quot;testName&quot;)
  Map&lt;String, Long&gt; procedure(@Param(&quot;data&quot;) String data);
}

When I start application:

IllegalArgumentException: Failed to create query for method public abstract java.util.Map TestEntityRepository.procedure(java.lang.String)! 
No property procedure found for type TestEntity

If i use stored procedure with only 1 output parameter everything is fine.

答案1

得分: 1

spring-data-jpa:2.2.0.RELEASE 之前的版本中,只能使用 @Procedure 注解来获取一个输出参数。

如果您不能更改依赖版本,那么您需要使用以下代码使其工作:

StoredProcedureQuery query = this.em.createNamedStoredProcedureQuery("testName");
query.setParameter("data", "data_value");
query.execute();
Long sum = (Long) query.getOutputParameterValue("res1");
Long sum = (Long) query.getOutputParameterValue("res2");
英文:

Only one out parameter can be fetched with @Procedure annotation prior to spring-data-jpa:2.2.0.RELEASE.

If you can't change the dependency version then you will have to make it work with following code :

StoredProcedureQuery query = this.em.createNamedStoredProcedureQuery(&quot;testName&quot;);
query.setParameter(&quot;data&quot;, &quot;data_value&quot;);
query.execute();
Long sum = (Long) query.getOutputParameterValue(&quot;res1&quot;);
Long sum = (Long) query.getOutputParameterValue(&quot;res2&quot;);

huangapple
  • 本文由 发表于 2020年7月23日 22:27:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63056624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定