oracle 通过 Hibernate 获取 PL/SQL 函数的返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1068974/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:30:20  来源:igfitidea点击:

Getting the return value of a PL/SQL function via Hibernate

oraclehibernateplsql

提问by Thomas L?tzer

I have a PL/SQL function in an Oracle database that I can't change. This function takes a parameter which identifies an entity, creates a copy of that entity and then returns the ID of the copy. This looks something like

我在 Oracle 数据库中有一个无法更改的 PL/SQL 函数。此函数采用一个参数来标识实体,创建该实体的副本,然后返回副本的 ID。这看起来像

FUNCTION copy_entity(id IN NUMBER) RETURN NUMBER

功能 copy_entity(id IN NUMBER) 返回编号

I need to call this function from Hibernate. I tried creating a named SQL query with something similar to

我需要从 Hibernate 调用这个函数。我尝试使用类似的东西创建一个命名的 SQL 查询

CALL copy_entity(:id)

CALL copy_entity(:id)

as the query, but from this I can't seem to get the return value of the function. Hibernate's "return-scalar" and similar options require a column name to return and I don't have a column name. This lead me to

作为查询,但由此我似乎无法获得函数的返回值。Hibernate 的“返回标量”和类似选项需要返回列名,而我没有列名。这导致我

SELECT copy_entity(:id) AS newEntityId

SELECT copy_entity(:id) AS newEntityId

with "return-scalar" using newEntityId as column name, but this also did not work since Oracle then throws an exception that I can't call INSERT (to save the copy) in a SELECT.

使用 newEntityId 作为列名的“返回标量”,但这也不起作用,因为 Oracle 然后抛出一个异常,我无法在 SELECT 中调用 INSERT(以保存副本)。

Is there any way to get the return value of such a PL/SQL function? The function is actually much more complex and still required in other parts of the app, so re-writing it is not really an option.

有没有办法得到这样一个PL/SQL函数的返回值?该功能实际上要复杂得多,并且在应用程序的其他部分仍然需要,因此重新编写它并不是一个真正的选择。

回答by Theo

I hope/think you can use an anonymous PL/SQL block:

我希望/认为您可以使用匿名 PL/SQL 块:

begin :myresult = copy_entity(:id); end;

开始:myresult = copy_entity(:id); 结尾;

Now you have 'column name' myresult with the result.

现在你有'列名' myresult 和结果。

I've never used hibernate so I hope it works. I don't know how flexible Hibernate is.

我从未使用过休眠,所以我希望它有效。我不知道 Hibernate 有多灵活。

回答by Adam Hawkes

I think you are stuck using straight JDBC. The Hibernate documentation has this in the limitations section for Oracle:

我认为您一直在使用直接 JDBC。Hibernate 文档在 Oracle 的限制部分中有这个:

For Oracle the following rules apply:

A function must return a result set. The first parameter of a procedure must be an OUT that returns a result set. This is done by using a SYS_REFCURSOR type in Oracle 9 or 10. In Oracle you need to define a REF CURSOR type. See Oracle literature for further information.

对于 Oracle,以下规则适用:

一个函数必须返回一个结果集。过程的第一个参数必须是返回结果集的 OUT。这是通过在 Oracle 9 或 10 中使用 SYS_REFCURSOR 类型来完成的。在 Oracle 中,您需要定义一个 REF CURSOR 类型。有关详细信息,请参阅 Oracle 文献。

Since this function accepts a number and returns a number you are out of luck with Hibernate and would probably be better off making a simple JDBC CallableStatement.

由于此函数接受一个数字并返回一个数字,因此您对 Hibernate 不走运,最好制作一个简单的 JDBC CallableStatement