java JPA 和 SYS_REFCURSOR 类似 OUT 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12009418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA and SYS_REFCURSOR like OUT parameter
提问by Paul Vargas
I want to call a procedure using JPA with SYS_REFCURSOR
like OUT
parameter. This is very easy using plain JDBC but I'm not sure that is possible in JPA.
我想使用带有SYS_REFCURSOR
类似OUT
参数的JPA 调用过程。使用普通 JDBC 非常容易,但我不确定在 JPA 中是否可行。
My procedure is like following:
我的程序如下:
CREATE OR REPLACE FUNCTION FN_GET_COINS
RETURN SYS_REFCURSOR
IS vCursor SYS_REFCURSOR;
BEGIN
OPEN vCursor FOR
SELECT
...
RETURN vCursor;
CLOSE vCursor;
EXCEPTION
...
END FN_GET_COINS;
回答by Glen Best
JPA 2.0has no support for stored procedures, but support has been added in JPA 2.1, part of Java EE 7. Examples of standard JPA 2.1 code using Oracle SYS_REF_CURSOR:
JPA 2.0不支持存储过程,但在JPA 2.1(Java EE 7 的一部分)中添加了支持。使用 Oracle SYS_REF_CURSOR 的标准 JPA 2.1 代码示例:
http://wiki.eclipse.org/EclipseLink/Release/2.5/JPA21#Ref_cursor_Example
http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#JPA_2.1_StoredProcedureQuery
http://wiki.eclipse.org/EclipseLink/Release/2.5/JPA21#Ref_cursor_Example
http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#JPA_2.1_StoredProcedureQuery
EclipseLink 2.5 supports JPA 2.1 (it's the reference implementation):
http://www.eclipse.org/eclipselink/releases/2.5.php
https://glassfish.java.net/Hibernate 4.3.11 supports JPA 2.1:
Hibernate up to 4.2 supports stored procedures via native API & config (non-JPA) :
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#sp_query
(Here the CURSOR must be either returned via a 'stored function', or must be the first parameter (out) of a stored procedure).
Additionally, Polpan's answer hereshow how this can be done with a JPA 2.0 native query with a QueryHint, setting Hibernate proprietary hint property
org.hibernate.callable
to true.
EclipseLink 2.5 支持 JPA 2.1(它是参考实现):
http://www.eclipse.org/eclipselink/releases/2.5.php
https://glassfish.java.net/Hibernate 4.3.11 支持 JPA 2.1:
Hibernate 高达 4.2 通过本机 API 和配置(非 JPA)支持存储过程:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#sp_query
(此处 CURSOR 必须通过“存储函数”返回,或者必须是存储过程的第一个参数(输出)。
此外,此处Polpan 的回答展示了如何使用带有 QueryHint 的 JPA 2.0 本机查询完成此操作,将 Hibernate 专有提示属性设置
org.hibernate.callable
为 true。
回答by DataNucleus
If you want to do it with standardised JPA 2.0 then you're out of luck (apart from hacking it through the "native query" API ... it is standardised in JPA2.1.
如果您想使用标准化的 JPA 2.0 来完成它,那么您就不走运了(除了通过“本机查询”API 对其进行黑客攻击之外……它在 JPA2.1 中是标准化的。
DataNucleus JPA has supported the JPA 2.1 syntax since early 2012 (as said in the original answer, so no idea what the "current answer is out of date" is supposed to mean), shown in these docs http://www.datanucleus.org/products/accessplatform_3_3/jpa/stored_procedures.html
DataNucleus JPA 自 2012 年初开始支持 JPA 2.1 语法(如原始答案中所述,因此不知道“当前答案已过时”是什么意思),如这些文档 http://www.datanucleus 中所示。 org/products/accessplatform_3_3/jpa/stored_procedures.html