wood burning stoves*
The moose likes Object Relational Mapping and the fly likes does Hibernate supports Stored Procedures? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "does Hibernate supports Stored Procedures?" Watch "does Hibernate supports Stored Procedures?" New topic
Author

does Hibernate supports Stored Procedures?

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

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

Joined: Aug 26, 2006
Posts: 4968
    
    1

"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

Joined: Aug 01, 2006
Posts: 401

thank you very much sir, i appreciate your for your response,
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Kudos really should go to Hongliang Li for the great tutorial!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: does Hibernate supports Stored Procedures?