英文:
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 = "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
@Repository
public interface TestEntityRepository extends JpaRepository<TestEntity, Long> {
@Transactional
@Procedure(name = "testName")
Map<String, Long> procedure(@Param("data") 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("testName");
query.setParameter("data", "data_value");
query.execute();
Long sum = (Long) query.getOutputParameterValue("res1");
Long sum = (Long) query.getOutputParameterValue("res2");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论