简体   繁体   English

使用createNativeQuery调用Oracle存储过程

[英]Call Oracle Stored Procedure Using createNativeQuery

I need to call a stored procedure using JPA and found this article: 我需要使用JPA调用存储过程并找到这篇文章:

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

which explains how to use EntityManager.createNativeQuery. 这解释了如何使用EntityManager.createNativeQuery。 However, the example actually calls a function that has a return argument. 但是,该示例实际上调用了一个具有return参数的函数。 I've tried searching for an example of calling a stored procedure that doesn't have a return, but haven't been able to find anything. 我已经尝试搜索一个调用没有返回但仍无法找到任何内容的存储过程的示例。

Can I use createNativeQuery to call a procedure? 我可以使用createNativeQuery来调用过程吗? Or does the procedure need to be modified to be a function (perhaps returns a success/failure result)? 或者是否需要将过程修改为函数(可能返回成功/失败结果)?

Thanks! 谢谢!

From the JPA wiki: 来自JPA维基:

1.4 Stored Procedures 1.4存储过程

JPA does not have any direct support for stored procedures. JPA对存储过程没有任何直接支持。 Some types of stored procedures can be executed in JPA through using native queries. 可以通过使用本机查询在JPA中执行某些类型的存储过程。 Native queries in JPA allow any SQL that returns nothing, or returns a database result set to be executed. JPA中的本机查询允许任何不返回任何内容的SQL,或返回要执行的数据库结果集。 The syntax to execute a stored procedure depends on the database. 执行存储过程的语法取决于数据库。 JPA does not support stored procedures that use OUTPUT or INOUT parameters. JPA不支持使用OUTPUT或INOUT参数的存储过程。 Some databases such as DB2, Sybase and SQL Server allow for stored procedures to return result sets. 某些数据库(如DB2,Sybase和SQL Server)允许存储过程返回结果集。 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不允许返回结果集,只返回OUTPUT参数,但确实定义了可以作为OUTPUT参数返回的CURSOR类型。 Oracle also supports stored functions, that can return a single value. Oracle还支持存储函数,可以返回单个值。 A stored function can normally be executed using a native SQL query by selecting the function value from the Oracle DUAL table. 通常可以通过从Oracle DUAL表中选择函数值,使用本机SQL查询来执行存储函数。

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. 一些JPA提供程序扩展了对存储过程的支持,一些还支持使用存储过程或自定义SQL覆盖实体的任何CRUD操作。 Some JPA providers have support for CURSOR OUTPUT parameters. 一些JPA提供程序支持CURSOR OUTPUT参数。

Example executing a stored procedure on Oracle 在Oracle上执行存储过程的示例

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

So my advices would be: 所以我的建议是:

  • do some experimentations (ie try it) 做一些实验(即试试)
  • if required (and if possible) modify the stored procedure 如果需要(如果可能),修改存储过程
  • consider provider specific extensions (as last resort) 考虑提供商特定扩展(作为最后的手段)

If it is possible, you'll likely need to wrap the procedure call this way: 如果可能,您可能需要以这种方式包装过程调用:

em.createNativeQuery("BEGIN yourprocedure; END;")

Getting return values back may be problematic with procedures. 通过程序获得返回值可能会有问题。 Passing them in should be easy. 传递它们应该很容易。

As already stated, the JPA spec does not yet support StoredProcedures, however the EclipseLink JPA provider does: 如前所述,JPA规范尚不支持StoredProcedures,但EclipseLink JPA提供程序执行:

http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Extensions_for_Stored_Procedure_Query http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Extensions_for_Stored_Procedure_Query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM