gpt4 book ai didi

java - 使用 createNativeQuery 调用 Oracle 存储过程

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:09:23 33 4
gpt4 key购买 nike

我需要使用 JPA 调用存储过程并找到这篇文章:

http://www.oracle.com/technology/pub/articles/vasiliev-jpql.html

解释了如何使用 EntityManager.createNativeQuery。但是,该示例实际上调用了一个具有返回参数的函数。我试过搜索调用没有返回值的存储过程的示例,但没有找到任何内容。

我可以使用 createNativeQuery 来调用过程吗?或者是否需要将过程修改为函数(可能返回成功/失败结果)?

谢谢!

最佳答案

来自 JPA wiki:

1.4 Stored Procedures

JPA does not have any direct support for stored procedures. Some types of stored procedures can be executed in JPA through using native queries. Native queries in JPA allow any SQL that returns nothing, or returns a database result set to be executed. The syntax to execute a stored procedure depends on the database. JPA does not support stored procedures that use OUTPUT or INOUT parameters. Some databases such as DB2, Sybase and SQL Server allow for stored procedures to return result sets. Oracle does not allow results sets to be returned, only OUTPUT parameters, but does define a CURSOR type that can be returned as an OUTPUT parameter. Oracle also supports stored functions, that can return a single value. A stored function can normally be executed using a native SQL query by selecting the function value from the Oracle DUAL table.

Some JPA providers have extended support for stored procedures, some also support overriding any CRUD operation for an Entity with a stored procedure or custom SQL. Some JPA providers have support for CURSOR OUTPUT parameters.

Example executing a stored procedure on Oracle

EntityManager em = getEntityManager();
Query query = em.createNativeQuery("BEGIN VALIDATE_EMP(P_EMP_ID=>?); END;");
query.setParameter(1, empId);
query.executeUpdate();

所以我的建议是:

  • 做一些实验(即尝试)
  • 如果需要(并且可能的话)修改存储过程
  • 考虑提供商特定的扩展(作为最后的手段)

关于java - 使用 createNativeQuery 调用 Oracle 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3180759/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com