Question: How to call a a MySql Stored Procedure from Hibernate?
Answer:
If your stored procedure is returning a value you can directly use <sql-query>. If not, you need to use jdbc connection to call the native sql procedure.. here is the code snippet...
// Insert this in your related hibernate java file..
public void someMethod() throws SQLException{
Session sess = getHibernateTemplate().getSessionFactory().openSession();
/*if you are not using session factory, you can use ur own method to get session.*/
Connection con = sess.connection();
String sqlQuery = "call VersionUpdateProcedure()";
CallableStatement cs = con.prepareCall(sqlQuery);
cs.execute();
con.close();
sess.close();
}
// In your implementation java file, where you want to call your procedure... insert this code...
try{ baseDao.versionUpdateQuery(); }
catch(SQLException s) { System.out.println(s);}
No need to specify anything like <sql-query> in the .hbm.xml...
Compile and Execute it.. It will call your stored procedure.
Author: Sandeep Tagore, India
|