【发布时间】: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-bom: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