简体   繁体   English

Oracle存储过程,SYS_REFCURSOR和NHibernate

[英]Oracle stored procedures, SYS_REFCURSOR and NHibernate

I have a legacy Oracle (10.2g) database that I'm connecting to and I'd like to use NHibernate (2.0.1) to give me back objects from a stored procedure. 我有一个遗留的Oracle(10.2g)数据库,我正在连接,我想使用NHibernate(2.0.1)从存储过程中返回对象。 The stored procedure in question uses a SYS_REFCURSOR to return results. 有问题的存储过程使用SYS_REFCURSOR返回结果。 According to the documentation this should be doable but I've found a few posts on the internet that suggest otherwise. 根据该文件 ,这应该是可行的,但我已经发现了几个 帖子暗示,否则在互联网上。

Here's my paraphrased code: 这是我的释义代码:

Mapping file: 映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
assembly="OracleStoredProcedures" namespace="OracleStoredProcedures">
    <class name="Person" mutable="false">
        <id name="PersonCode" type="AnsiString" column="PERSONCODE">
            <generator class="assigned" />
        </id>
        <property name="Name" type="String" column="PERSON_NAME" />
        <property name="Surname" type="String" column="PERSON_SURNAME" />
    </class>

    <sql-query name="getpeople">
        <return class="Person" />

        EXEC RS_DB.GETPERSONTEST 

    </sql-query>
</hibernate-mapping>

Stored procedure: 存储过程:

CREATE OR REPLACE PROCEDURE RS_DB.GETPERSONTEST (
   io_cursor   IN OUT   sys_refcursor
)
IS
BEGIN
   OPEN io_cursor FOR
      SELECT PERSONCODE, PERSON_NAME, PERSON_SURNAME
      FROM PEOPLE

END GETPERSONTEST;

What a royal pain this was. 这是多么皇家的痛苦。 This finally worked. 这终于奏效了。 I turned the store procedure into a function. 我把商店程序变成了一个功能。 Function returned sys_refcursor. 函数返回sys_refcursor。 Used similar mapping as the OP and name query as below. 使用与OP和名称查询类似的映射,如下所示。

<sql-query name="getpeople">
 <return class="Person" />

 { ? = call RS_DB.GETPERSONTEST }
</sql-query>

Link 链接

In your hibernate you state a return type, but Oracle procedures don't return anything. 在你的休眠中,你声明了一个返回类型,但Oracle程序不会返回任何内容。 Perhaps if you changed it to a function which returned the ref cursor it would work properly. 也许如果你把它改成一个返回引用光标的函数,它将正常工作。 Also, I believe CALL is the proper syntax. 另外,我相信CALL是正确的语法。 EXEC is a SQL*Plus command and not really a SQL statement. EXEC是SQL * Plus命令,而不是SQL语句。

As far as I remember this was a bug I also found a couple of years ago when working with oracle, I've tracked back the issue in NH tracker and is fixed but on version 2.1.1GA; 据我记得,这是我几年前在与oracle合作时发现的一个错误,我在NH跟踪器中追踪了这个问题并且修复了但是版本为2.1.1GA; Can you verify that this is the same problem you have? 你能证实这是同样的问题吗? https://nhibernate.jira.com/browse/NH-847 https://nhibernate.jira.com/browse/NH-847

I came across this same problem today. 我今天遇到了同样的问题。 For us, the fix was to use "CALL" rather than "EXEC", add the round brackets "()" to the procedure call, and wrap the call in curly braces "{}": 对我们来说,修复是使用“CALL”而不是“EXEC”,将圆括号“()”添加到过程调用中,并用大括号“{}”包装调用:

<sql-query name="getpeople">
    <return class="Person" />

    { CALL RS_DB.GETPERSONTEST() }

</sql-query>

According to this page you should use CALL instead of EXEC. 根据此页面,您应该使用CALL而不是EXEC。 I have not tried this, so YMMV. 我没试过这个,所以YMMV。

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

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