从存储过程获取输出参数,而无需调用execute()

编程入门 行业动态 更新时间:2024-10-25 12:28:03
本文介绍了从存储过程获取输出参数,而无需调用execute()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过实体管理器从Java程序中调用PL / SQL存储过程:

I want to call a PL/SQL stored procedure from within a Java program via an entity manager:

StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("someProcedure");

现在,我的理解是我必须在此storageProcedureQuery上调用execute()才能执行过程并能够检索OUT参数。但是,我可以无需检索这些值,而只需调用

Now my understanding was that I have to call execute() on this storedProcedureQuery in order to execute the procedure and be able to retrieve the OUT-parameters. However, I can retrieve these values without calling the execute()-Method by simply calling

someValue1 = (Integer)storedProcedureQuery.getOutputParameterValue(1); someValue2 = (Integer)storedProcedureQuery.getOutputParameterValue(2);

这怎么可能?此代码段何时确切执行存储过程?调用getOutputParameterValue()时,是否会每次执行该过程?有任何官方文件证明这种行为吗?

How is this possible ? When exactly will the stored procedure be executed in this code snippet? Will the procedure be executed every time when getOutputParameterValue() is called ? Is there any official documentation for this behaviour ?

推荐答案

我是该领域的新手,对这个问题像您一样好奇。但是我利用这次机会进行了测试,下面是我的观察

I am a new learner in his field and as curious as you with the question. However i have taken this opportunity to test it and below is my observation,

According to the documentation execute() returns: > Return true if the first result corresponds to a result set, > and false if it is an update count or if there are no results > other than through IN OUT and OUT parameters, if any.

因此,我会说,返回的true或false并不表示成功或不执行。

therefore I would say, the returned true or false doesn't mean a successful or not execution.

同样,我们必须在调用 getOutputParameterValue 之前注册参数。如果我们研究 getOutputParameterValue 的实现,我们将能够准确找到休眠提供程序(在我的情况下为JPA)调用实际执行的位置。

Again we must register the parameters before we call to getOutputParameterValue. If we looked into the implementation of getOutputParameterValue, we would able to find exactly where the hibernate provider ( which is JPA in case of mine) calls to the actual execution.

进一步执行了多少次,我通过将其插入到调用过程中的另一个表中的方式来对其进行测试。

Further to how many times the execution happened I tested it in a way to check it by inserting to another table inside the calling procedure.

create table test_procedure_call(msg varchar2(100)); CREATE OR REPLACE PROCEDURE test ( p_in_1 IN NUMBER, p_out_1 OUT VARCHAR2, p_out_2 OUT VARCHAR2 ) AS BEGIN insert into test_procedure_call values ('Executed..'); commit; select 'FirstName'||' '||'LastName','HR' into p_out_1,p_out_2 from dual where p_in_1=1; END; / @Test public void testStoredProcedureQuery() { StoredProcedureQuery sp = em.createStoredProcedureQuery("test"); // set parameters sp.registerStoredProcedureParameter("p_in_1", Integer.class, ParameterMode.IN); sp.registerStoredProcedureParameter("p_out_1", String.class, ParameterMode.OUT); sp.registerStoredProcedureParameter("p_out_2", String.class, ParameterMode.OUT); sp.setParameter("p_in_1", 1); String name = sp.getOutputParameterValue("p_out_1").toString(); String dept = sp.getOutputParameterValue("p_out_2").toString(); System.out.println("Name : " + name); System.out.println("Department : " + dept); } select * from test_procedure_call; MSG ---------------------------------------------------------------------------------------- Executed..

通过表test_procedure_call的调用,我们可以确认其每次测试仅执行一次。 (如上例所示)。

By this out of the table test_procedure_call, we can confirm it executes only once per test. (as we saw in above example).

更多推荐

从存储过程获取输出参数,而无需调用execute()

本文发布于:2023-10-28 23:16:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存储过程   参数   execute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!