使用xml配置调用存储过程Spring Data JPA

时间:2017-07-27 10:36:17

标签: spring jpa spring-data-jpa

wikibooks表示可以在xml中使用 named-stored-procedure-query 来通过JPA 2.1调用存储过程。

我试过用 org.springframework.data:spring-data-jpa:jar:< strong> 1.11.4.RELEASE (由解决方案) io.spring.platform:platform-b​​om:Brussels-SR3 )实现同样的目标。

程序定义:

create or replace PROCEDURE PRC_DEL_LOC (p_LocationID int, p_BusinessID int, p_Status  out int)
 AS    BEGIN
  DELETE FROM  MultiLocation_XXX WHERE LocationID = p_LocationID;
  p_Status := p_BusinessID;
END;

存储库界面

@Repository
public interface BusinessTypesJTARespository extends JpaRepository<BusinessTypesJTA, Integer> {

 @Transactional
 @Procedure(procedureName = "PRC_DEL_LOC", name = "DEL_MultiLocation_MBA", outputParameterName = "p_Status")
int inAndOutTest(@Param("p_LocationID") 
Integer inParam1, @Param("p_BusinessID") Integer inParam2);
 }

orm.xml中

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm
 http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd"
version="2.1">
 <named-stored-procedure-query name="DEL_MultiLocation_MBA"
    procedure-name="PRC_DEL_LOC">
    <parameter class="java.lang.Integer" mode="IN" name="p_LocationID" />
    <parameter class="java.lang.Integer" mode="IN" name="p_BusinessID" />
    <parameter class="java.lang.Integer" mode="OUT" name="p_Status" />
    <!--  <result-class>com.memorynotfound.hibernate.Book</result-class> -->
  </named-stored-procedure-query>
 </entity-mappings>

在调试过程中我发现(参见StoredProcedureAttributeSource.createFrom(...)),Spring代码似乎期望相关的存储库方法声明用 NamedStoredProcedureQueries NamedStoredProcedureQuery 进行注释。

如果没有任何一个注释(如我的情况),则忽略 outputParameterName 的提供值&amp;设置为&#34; out &#34;由StoredProcedureAttributes的构造函数。

结果,我正在

  org.springframework.dao.InvalidDataAccessResourceUsageException: Error calling CallableStatement.getMoreResults; SQL [PRC_DEL_LOC]; nested exception is org.hibernate.exception.SQLGrammarException: Error calling CallableStatement.getMoreResults

  Caused by: java.sql.SQLException: ORA-06550: line 1, column 7:
  PLS-00306: wrong number or types of arguments in call to 'PRC_DEL_LOC'
  ORA-06550: line 1, column 7:
  PL/SQL: Statement ignored.

那么,是否不能仅使用xml配置调用存储过程,而不使用注释 EntityManager

编辑(进一步观察):

当我将以下内容添加到@Entity类中时,一切正常。

@NamedStoredProcedureQueries({
 @NamedStoredProcedureQuery(
        name = "DEL_MultiLocation_MBA",
        procedureName = "PRC_DEL_LOC",     //  resultClasses = Book.class,
        parameters = {
                @StoredProcedureParameter(
                        name = "p_Status",
                        mode = ParameterMode.OUT,
                        type = Integer.class)
        }
     )
  })

1 个答案:

答案 0 :(得分:0)

请尝试将方法更改为:

@Procedure(procedureName = "PRC_DEL_LOC")
int inAndOutTest(Integer inParam1, Integer inParam2);

我认为您可以直接映射到JPA存储库

中的数据库中的过程
  @Procedure
  returnType procedureName(InArgs...);