calling oracle procedure using hibernate
Hi,
I want to call a oracle procedure using hibernate. I just want my procedure to do some processing and has no out parameter. But as I have gathered from the docs -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
I have made my procedure with an out parameter satisfing the above requirement.
Procedure:
CREATE OR REPLACE
PROCEDURE SPC_USER_DETAILS (User_cv OUT SYS_REFCURSOR) AS
v_User_idNumber(38);
v_FIRST_NAMEVARCHAR2(255);
v_LAST_NAMEVARCHAR2(255);
v_emailVARCHAR2(255);
BEGIN
OPEN User_cv FOR
SELECT * FROM contact;
loop
fetch User_cv into v_User_id, v_FIRST_NAME,v_LAST_NAME,v_email;
exit when User_cv%notFound;
insert into dummy_contact values (v_User_id, v_FIRST_NAME,v_LAST_NAME,v_email);
end loop;
close User_cv;
End;
Java code calling the procedure.
tx = session.beginTransaction();
session.getNamedQuery("prash_test");
hibernate.cfg.xml
<mapping resource="hibernate/test.hbm.xml" />
test.hbm.xml
<hibernate-mapping>
<sql-query name="prash_test" callable="true">
<return class="com.ni.genreg.presentation.formbeans.TestForm">
</return>
{ call SPC_USER_DETAILS(?) }
</sql-query>
</hibernate-mapping>
On running my java class, that calls the procedure, I dont get any errors. But the changes are not getting reflected in the database. I have tested the oracle procedure, its in working fine.
Any idea what I could be doing wrong in this approach?
Thanks in advance.
# 4
on writing :
List list =session.getNamedQuery("prash_test").list();
I get some exception: Cursor is closed.
But my procedure is working fine otherwise.
Hibernate: { call SPC_USER_DETAILS(?) }
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)
at org.hibernate.loader.Loader.list(Loader.java:2023)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:150)
at hibernate.TestExample.main(TestExample.java:42)
Caused by: java.sql.SQLException: Cursor is closed.
at oracle.jdbc.driver.T4CResultSetAccessor.getCursor(T4CResultSetAccessor.java:240)
at oracle.jdbc.driver.ResultSetAccessor.getObject(ResultSetAccessor.java:94)
at oracle.jdbc.driver.OracleCallableStatement.getObject(OracleCallableStatement.java:1357)
at org.hibernate.dialect.Oracle9Dialect.getResultSet(Oracle9Dialect.java:285)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:193)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1665)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2144)
... 7 more
23:00:28,200 WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: null
23:00:28,200 ERROR JDBCExceptionReporter:72 - Cursor is closed.