Calling Stored Procedures in Hibernate

By |2018-03-13T03:38:16+05:30December 27th, 2015|Hibernate|

In this tutorial, we are going to access the MySql stored procedures in hibernate application. To do so, we need to create a procedure in Mysql. Here is the code for creating the procedure in MySql.

Creating Procedure in MySql :

CREATE PROCEDURE getAllEmployees()
SELECT *  FROM employee;
END //

Now, we are going to call the above procedure from hibernate application. Here is the example.

Calling Stored Procedures in Hibernate :

Project Structure :

Stored Procedures in Hibernate Example

Required Dependencies:


        <!-- Hibernate -->
        <!-- MySQL Driver -->

Hibernate Pojo  Class :

package com.onlinetutorialspoint.hibernate.pojo;

public class Employee {
    private int employeeId;
    public int getEmployeeId() {
        return employeeId;
    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    public String getEmployeeName() {
        return employeeName;
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    public int getDepartmentNo() {
        return departmentNo;
    public void setDepartmentNo(int departmentNo) {
        this.departmentNo = departmentNo;
    private String employeeName;
    private int departmentNo;

Hibernate Mapping File :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    <class name="com.onlinetutorialspoint.hibernate.pojo.Employee" table="employee">
        <id name="employeeId" column="empid" />
        <property name="employeeName" column="empname" />
        <property name="departmentNo" column="deptno" />
    <sql-query name="sQuery" callable="true">
    <return class="com.onlinetutorialspoint.hibernate.pojo.Employee"  alias="e"/>
    call getAllEmployees()

package com.onlinetutorialspoint.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private HibernateUtil() {


    private static SessionFactory sessionFactory;

    public static synchronized SessionFactory getInstnce() {

        if (sessionFactory == null) {
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
            sessionFactory = configuration.buildSessionFactory(;
        return sessionFactory;

Run the Application :

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.onlinetutorialspoint.hibernate.pojo.Employee;
import com.onlinetutorialspoint.util.HibernateUtil;

public class Main {

    public static void main(String[] args) {

        SessionFactory sessionFactory = HibernateUtil.getInstnce();
        Session session = sessionFactory.openSession();
        Query query = session.getNamedQuery("sQuery");
        List employeeList = query.list();
        Iterator employeeItr = employeeList.iterator();

        while (employeeItr.hasNext()) {
            Employee employee = (Employee);
            System.out.println("Employee id : " + employee.getEmployeeId());



Output :

Hibernate: call getAllEmployees()
Employee id : 10
Employee id : 20
Employee id : 30
Employee id : 101

Happy Learning 🙂

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment