Stored procedure example in Hibernate

This tutorial shows how we can call stored procedure in Hibernate API.


Hibernate 3.x jars, mysql-connector-java jar, jdk 1.6, MySQL 5.x

stored procedure example in hibernate

1. Create a Store Procedure in MySQL in database cdcol


2. Create hibernate.cfg.xml file with the following content and put the file under classpath


3. Create hibernate.reveng.xml file with the following content and put the file under classpath


4. Create the hibernate mapping file and domain object for cds table which is in cdcol database



5. Create the for session factory


6. Now create the helper class which will query for invoking store procedure. I will show here two approaches – native sql and named query.


7. Now create a main class for testing both the approaches.




That’s all. Thank your for your patience. Please do not forget to leave a comment.

2 Replies to “Stored procedure example in Hibernate”

  1. Hi

    From the Hibernate docs, I could see that it is recommended to use commit. Any specific reason why you didn’t commit the transaction.

    I’m having a problem where commit of get storedprocedure auto invoking update which I don’t want. I guess this is due to flush but I don’t want this to happen.

    1. the call to close() commits the transaction, that’s why I did not commit. You can use the property false in hibernate configuration to prevent autocommit and use the commit() in java code.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.