Saturday 22 February 2014

calling procedure using hibernate



Hello Friends.hope you all are fine .today i am going to demonstrate how you can call procedure using hibernate native query .well that's all about what this post all about .no much description.detail code is as given below :


procedure that i have written into mysql is as given below.procudure takes one parameter and retruns related student data :

DELIMITER $$

DROP PROCEDURE IF EXISTS `getStudent` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getStudent`(id VARCHAR(20))
BEGIN

SELECT * FROM student WHERE id = id;
END $$

DELIMITER ;



package org.dao;

import java.util.List;

import org.entity.Student;
import org.hibernate.Query;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;


public class ProcedureCall
{
   
public HibernateTemplate hibernatetemplate;
   
    public HibernateTemplate getHibernatetemplate() {
        return hibernatetemplate;
    }

    public void setHibernatetemplate(HibernateTemplate hibernatetemplate) {
        this.hibernatetemplate = hibernatetemplate;
    }

    public ProcedureCall(HibernateTemplate hibernatetemplate)
    {
    this.hibernatetemplate=hibernatetemplate;
    }
   
    public List<Student> getStudentData()
    {
       
       
        Query query = getHibernatetemplate().getSessionFactory().openSession().createSQLQuery("CALL studentdb.getStudent(:id)")
                .addEntity(Student.class)
                .setParameter("id", "1");
           
            List<Student> result = query.list();
            for(int i=0; i<result.size(); i++){
                Student student = (Student)result.get(i);
                System.out.println(student.getId());
               
            }
        return result;
    }
   
    public static void main(String[] args) {
       
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context-conf.xml");
       
       
            ProcedureCall call  =(ProcedureCall) context.getBean("ProcedureCall");
           
            List<Student> result;
           
            result= call.getStudentData();
           
            for(int i=0;i<result.size();i++)
            {
               
                System.out.println("ID:"+result.get(i).getId());
                System.out.println("NAME:"+result.get(i).getName());
            }
           
    }

}

Configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/mvc   
     http://www.springframework.org/schema/mvc/spring-mvc.xsd">



<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/studentdb" />
        <property name="username" value="root" />
        <property name="password" value="admin!@#"/>
</bean>

    <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />

        <property name="annotatedClasses" value="org.entity.Student">
           </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">none</prop>
            </props>
        </property>
    </bean>
   
    <bean id="ProcedureCall" class="org.dao.ProcedureCall">
        <constructor-arg ref="hibernateTemplate" />
    </bean>
   
   
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <constructor-arg ref="mySessionFactory" />
    </bean>
</beans>

i hope you will get my point.
for query ping me on pathak.nisarg@yahoo.com
thanks for reading


No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...