24.HibernateTemplate

HibernateTemplate

Spring framework provides HibernateTemplate class
 Don’t worry about creating configuration, creating session factory, transaction, commits
Following are methods of HibernateTemplate class

1) void persist(Object entity)
Saves the given object.

2)Serializable save(Object entity)
Saves the given object and returns id.

3)void saveOrUpdate(Object entity)
Saves or updates the given object. If id is found, it updates the record otherwise saves the record.

4)void update(Object entity)
updates the given object.

5)void delete(Object entity)
deletes the given object on the basis of id.

6)Object get(Class entityClass, Serializable id)
returns the persistent object on the basis of given id.

7)Object load(Class entityClass, Serializable id)
returns the persistent object on the basis of given id.

8)List loadAll(Class entityClass)
returns the all the persistent objects.

Above are HibernateTemplate Methods.

For Spring+Hibernate integration we have to create datasource and pass hibernate.hbm.xml in different way

Example
package bo;

public class StudentBo {
       private int sno;
       private String name;
       private String address;
       public int getSno() {
              return sno;
       }
       public void setSno(int sno) {
              this.sno = sno;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
       public String getAddress() {
              return address;
       }
       public void setAddress(String address) {
              this.address = address;
       }

}


package dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import bo.StudentBo;

public class StudentDao {
      
       private HibernateTemplate template;

       public HibernateTemplate getTemplate() {
              return template;
       }

       public void setTemplate(HibernateTemplate template) {
              this.template = template;
       }
      
       //insert
       public void saveData(StudentBo bo){
              System.out.println("Saving : "+bo);
              template.save(bo);
       }
      
       //update
       public void updateData(StudentBo bo){
              System.out.println("updateData : "+bo);
              template.update(bo);
       }
      
       //delete
       public void deleteData(StudentBo bo){
              System.out.println("deleteData : "+bo);
              template.delete(bo);
       }
      
       //select all
       public List<StudentBo> selectAll(StudentBo bo){
              List<StudentBo>list = new ArrayList<StudentBo>();           
              list = template.loadAll(StudentBo.class);
              return list;
       }
      
       //select by id
       public StudentBo selectById(int sno){
              StudentBo ob =       template.get(StudentBo.class, sno);
              return ob;

       }     
}


package main;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import bo.StudentBo;

import dao.StudentDao;

public class StudentMain {
public static void main(String[] args) {
       BeanFactory factory = new ClassPathXmlApplicationContext("res/s.xml");
       StudentDao dao = (StudentDao) factory.getBean("dao");
      
       StudentBo bo = new StudentBo();
       bo.setSno(8);
       bo.setName("VIRODI");
       bo.setAddress("KOLKATTA");
      
       dao.saveData(bo);   
}
}


<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
 
<hibernate-mapping> 
<class name="bo.StudentBo" table="student"> 
          <id name="sno"> 
          <generator class="assigned"></generator> 
          </id>             
          <property name="name"></property> 
          <property name="address"></property> 
</class>             
</hibernate-mapping>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
                "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
       <!-- 1. set Properties to DriverManagerDataSource -->
       <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver" />
              <property name="url" value="jdbc:mysql://localhost:3306/test" />
              <property name="username" value="root" />
              <property name="password" value="root" />
       </bean>

       <bean id="mysessionFactory"       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
              <property name="dataSource" ref="ds"></property>
              <property name="mappingResources">
                     <list>
                           <value>student.hbm.xml</value>
                     </list>
              </property>

              <property name="hibernateProperties">
                     <props>
                           <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                           <prop key="hibernate.hbm2ddl.auto">update</prop>
                           <prop key="hibernate.show_sql">true</prop>
                     </props>
              </property>
       </bean>

       <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
              <property name="sessionFactory" ref="mysessionFactory"></property>
       </bean>

       <bean id="dao" class="dao.StudentDao">
              <property name="template" ref="template"></property>
       </bean>
</beans>

Post a Comment

Thank You

Previous Post Next Post