passing a Cursor as a parameter using hibernate query object to a stored procedure

I have a stored procedure which takes a cursor as parameter. I have a namedquery defined in hibernate mappings to call the stored procedure… I need to make a call it from a DAO using hibernateTemplate (spring, hibernate)

This is how I am doing it..

Query q =  (Query) hibernateTemplate.findByNamedQuery("getFromGTT", OracleTypes.CURSOR);

but it gives me some exception…

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.loader.Loader.doList(Loader.java:2214)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
    at org.hibernate.loader.Loader.list(Loader.java:2090)
    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 org.springframework.orm.hibernate3.HibernateTemplate$32.doInHibernate(HibernateTemplate.java:916)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:907)
    at dao.GTTDaoImpl$1.doInHibernate(GTTDaoImpl.java:27)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:338)
    at dao.GTTDaoImpl.getDataFromGTT(GTTDaoImpl.java:23)
    at test.TestOracleProcedures.test(TestOracleProcedures.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'GET_FROM_GTT'

need a solution.. Thanks

Answer

You should post your procedure spec for people to better help you, but since you mention passing in a cursor, you should know there are restrictions on calling stored procedures from Hibernate:

For Oracle the following rules apply:

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. In Oracle you need to define a REF CURSOR type. See Oracle literature for further information.

If you can’t meet these requirements, you can still invoke the procedure by invoking a native JDBC call.

Further, OracleTypes.CURSOR is a JDBC type, its not a bind variable. If your procedure needs a value to execute, you need to pass that actual value in to findByNamedQuery.

Leave a Reply

Your email address will not be published. Required fields are marked *