Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

does Hibernate supports Stored Procedures?

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

May i know if HB supports Strored Procedures or not? if not may i know why and how to handle the problem in projects?

thanks in advance,
rahul
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Hibernate 3 introduces support for queries via stored procedures and functions...The stored procedure/function must return a resultset as the first out-parameter to be able to work with Hibernate. "

Here's a good little article that I wish I wrote, but I didn't. Please check out the source, although I've thrown a liberal quote in here to give you a taste:

Tutorial on Hibernate and Stored Procedures

Call SQL Procedure with JDBC

The CallableStatement interface used to execute SQL stored procedures. The JDBC API provides a stored procedure SQL escape syntax that allows stored procedures to be called in a standard way for all RDBMSs. This escape syntax has one form that includes a result parameter and one that does not. If used, the result parameter must be registered as an OUT parameter. The other parameters can be used for input, output or both. Parameters are referred to sequentially, by number, with the first parameter being 1.



IN parameter values are set using the set methods inherited from PreparedStatement. The type of all OUT parameters must be registered prior to executing the stored procedure; their values are retrieved after execution via the get methods provided here.

In the preceding example in_user _id and out_username are the input and output parameters in the procedure respectively. Please also be aware that you can not use the Hibernate to call a procedure if there is an output parameter other than a result set.

If the resultSet has multiple records, you have to go through the each record.



Otherwise, you can just use callableStatement to fetch the value for the required column.
String username = callableStatement.getLong("out_ username ");


Tutorial on Hibernate and Stored Procedures
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much sir, i appreciate your for your response,
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kudos really should go to Hongliang Li for the great tutorial!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic