【问题标题】:calling Stored Procedure Spring Data JPA with xml configuration使用 xml 配置调用存储过程 Spring Data JPA
【发布时间】:2018-01-03 00:55:11
【问题描述】:

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

我尝试过使用 org.springframework.data:spring-data-jpa:jar: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 进行注释。。 p>

如果没有被其中任何一个注释(如我的情况),则提供的 outputParameterName 值将被 @ 的构造函数忽略并设置为“out” 987654323@.

结果,我得到了

  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.

那么,不使用 annotation 或 EntityManager 是否不能仅使用 xml 配置调用存储过程?

编辑(进一步观察):

当我将以下内容添加到 @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)
        }
     )
  })

【问题讨论】:

  • memorynotfound.com/… 说:“如果您更喜欢 XML 而不是 Annotations,您可以使用等效的 JPA XML 映射”

标签: spring jpa spring-data-jpa


【解决方案1】:

请尝试将方法更改为:

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

我认为您可以直接映射到 JPA 存储库中数据库中的过程

  @Procedure
  returnType procedureName(InArgs...);

【讨论】:

  • 请详细说明或分享链接
  • 我想在 orm.xml 中集中所有查询(命名、本机)和程序并保持 bean 干净。正是我的问题是避免 EntityManager & NamedStoredProcedureQuery!
  • 程序应保存在数据库中,并从您的应用程序代码中调用。
  • @fg78c,没错。但我想在 orm.xml 中声明 namedprocedure,而不是在实体 clss 中。这可能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 2015-12-26
  • 2014-09-22
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多