Hibernate Template



    What  is hibernate    template ?



1 ) The Spring framework provides HibernateTemplate class, so you don't need to follow so many steps like create Configuration, BuildSessionFactory, Session, beginning and committing transaction etc. Hibernate Template  class and Method  make shorten  the lengthy  code steps in Hibernate. Makes easy the basic CURD orations and  others  configuring context file. 
     Mapping    Step  HibernateTemplate  in  the  Spring Context file 


               1)  Create data source 

               2)  Configure LocalSessionFactoryBeans 

                   a)   Configure data source 

                   b)   Configure Mapping resources(hbm.xml file) like employee.hbm.xml
                   c)   Configure HibernateProperties like  dilect ,update etc 
                   d)   Configure   HibernateTemplate

          Here    is an example of   applicationContext.xml   which   describe the how we 
          configure and use hibernate in spring. It’s also explains use of Template 
       
         applicationContext.xml
------------------------------------------

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9.   
  10.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  11.  <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>  
  12.         <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
  13.         <property name="username" value="system"></property>  
  14.         <property name="password" value="oracle"></property>  
  15.     </bean>  
  16.       
  17.     <bean id="mysessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  18.         <property name="dataSource" ref="dataSource"></property>  
  19.           
  20.         <property name="mappingResources">  
  21.         <list>  
  22.         <value>employee.hbm.xml</value>  
  23.         </list>  
  24.         </property>  
  25.           
  26.         <property name="hibernateProperties">  
  27.             <props>  
  28.                 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>  
  29.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  30.                 <prop key="hibernate.show_sql">true</prop>  
  31.                   
  32.             </props>  
  33.         </property>  
  34.     </bean>  
  35.       
  36.     <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  37.     <property name="sessionFactory" ref="mysessionFactory"></property>  
  38.     </bean>  
  39.       
  40.     <bean id="d" class="com.blsoft.EmployeeDao">  
  41.     <property name="template" ref="template"></property>  
  42.     </bean>  
  43.       
  44.       
  45.     </beans>  


  In Java class   we can use   EmployeeDao  for  easy CURD opration using HibernateTemplate. Here assuming EmployeeDao and hbm  files are defined.

Hibernate Template Hibernate Template Reviewed by Mukesh Jha on 2:49 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.