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;
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With