These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Calling a MySql Stored Procedure from Hibernate with Code
PostPosted: Thu Aug 07, 2008 3:30 am 
Newbie

Joined: Thu Aug 07, 2008 3:15 am
Posts: 2
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


Top
 Profile  
 
 Post subject: calling stored procedures in hibernate
PostPosted: Thu Aug 07, 2008 3:30 am 
Newbie

Joined: Thu Aug 07, 2008 3:15 am
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.