Calling Stored Procedure from Hibernate mapping file
Mat Anthony
Ranch Hand
Joined: May 21, 2008
Posts: 195
posted
0
Hi,
I'm trying to call the following stored procedure from a hibernate mapping file.
Not sure if this is the correct way to call a stored procedure from Hiberanate.
All I want back is the String result back from the stored procedure.
In Hibernate to call a stored procedure, you can only return Reference cursors. And it must be the first out parameter.
Since you are using Spring, I recommend you look at the StoredProcedure class Spring has and use JDBCTemplate to run that code. I hate to say it this way, but because of some other vendor (Not Hibernate) not 100% implementing the JDBC spec caused all this trouble with Stored Procedures in Hibernate.
Hi Mark,
thanks for your response. I got the following error:-
Caused by: org.hibernate.MappingException: Named query not known: select PERSON_DETAILS(ID) from PERSON person where person.ID = 12345
at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:70)
I have implemented the following code:-
Mat
Mat Anthony
Ranch Hand
Joined: May 21, 2008
Posts: 195
posted
0
Hi,
can any body give me a help with this problem, I'm new to calling stored procedures via hibernate .
I have updated the original code to include the file: person.hbm.xml
The sql does not seem to get exceuted, hence result=SQLQueryImpl{ call PERSON_DETAILS(?, :perId) })
There are rules and limitations for using stored procedures with Hibernate as Mark said.
Suppose you are using oracle , 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 and you need to define a REF CURSOR type.
While these rules vary with different databases, if your functions or procedures do not satisfy these rules no matter what kind of changes have been done to your java codes or hbm.xml ,it won't work.
May be you could call stored procedure through JDBC connection(new Configuration().configure("hibernate.cfg.xml").buildSettings().getConnectionProvider().getConnection();) instead of Hibernate Session.