Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The stored procedure in question uses a SYS_REFCURSOR to return results. 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;
like image 383
Richard Nienaber Avatar asked Mar 11 '09 18:03

Richard Nienaber


People also ask

When to Use REF cursor in Oracle?

Using REF CURSOR s is one of the most powerful, flexible, and scalable ways to return query results from an Oracle Database to a client application. A REF CURSOR is a PL/SQL data type whose value is the memory address of a query work area on the database.

What is cursor and REF cursor in Oracle?

A cursor is really any SQL statement that runs DML (select, insert, update, delete) on your database. A ref cursor is a pointer to a result set. This is normally used to open a query on the database server, then leave it up to the client to fetch the result it needs.

What is REF cursor used for?

A cursor is a pointer to a result set for a query. By returning a sys_refcursor you allow the client to fetch as many or few of the rows from the query as it requires. In stateful applications this could be used to page through results.


2 Answers

What a royal pain this was. This finally worked. I turned the store procedure into a function. Function returned sys_refcursor. Used similar mapping as the OP and name query as below.

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

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

Link

like image 56
Deepfreezed Avatar answered Oct 24 '22 18:10

Deepfreezed


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; Can you verify that this is the same problem you have? https://nhibernate.jira.com/browse/NH-847

like image 41
Sebastian Piu Avatar answered Oct 24 '22 18:10

Sebastian Piu