Hibernate - SPLessons

Spring Hibernate Integration

Home > Lesson > Chapter 31
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring Hibernate Integration

Spring Hibernate Integration

shape Description

Spring Hibernate Integration, basically in hibernate application developer supposed to include configuration file to add the connections between database, application but while integrating spring with hibernate no need to use configuration file why because spring by default provides Hibernate Template, insted of using configuration file here applicationContext.xml The file will be used. The HibernateTemplate class is defined in org.springframework.orm.hibernate3.HibernateTemplate package. These HibernateTemplate classes contain some predefined methods like save(),saveOrUpdate(), delete(). For example, store the employee details in Database table using HibernateTemplate class.

shape Example

  • Create the project directory structure.
  • Create the persistence class.
Employee.java [java]package com.itoolsinfo; public class Employee { private int id; private String name; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } } [/java] Here created the class Employee by giving variables such as id, name, address. Set and Get methods are used to retrieve the those variables.
  • To map the persistence class in mapping file.
employee.hbm.xml [xml] <hibernate-mapping> <class name="com.iquickinfo.Employee" table="employee3"> <id name="id" column="id"> <generator class="assigned"></generator> </id> <property name="name" length="14"/> <property name="address" column="address"/> </class> </hibernate-mapping> [/xml] The generator class subelement of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the org.hibernate.id.IdentifierGenerator interface.
  1. Create the Dao class.
TemplateDaoClass.java [java]package com.itoolsinfo; import org.springframework.orm.hibernate3.HibernateTemplate; public class TemplateDaoClass { HibernateTemplate template; public HibernateTemplate getTemplate() { return template; } public void setTemplete(HibernateTemplate template) { this.template = template; } public void saveEmployeeDetails(Employee employee) { template.save(employee); } } [/java] The big advantage of spring is that it will provide hibernate template.
  • Configure all the hibernate properties, Database properties, mapping properties, Template class properties in applicationContext.xml file. It will read the spring container and perform the operations.
applicationContext.xml [xml] [xml]<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver&quot;></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property> <property name="username" value="system"></property> <property name="password" value="system"></property> </bean> </bean> <bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mappingResources"> <list> <value>employee.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"<org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.hbm2ddl.auto">create</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="templatedao" class="com.itoolsinfo.TemplateDaoClass"> <property name="template1" ref="template"></property> </bean> </beans> [/xml]
  • Create the main class and create the persistence class object and set the values.
EmployeeDetailsInsert.java [java]package com.itoolsinfo; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class EmployeeDetailsInsert { public static void main(String[] args) { Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory beanFactory= new XmlBeanFactory(resource); TemplateDaoClass dao= (TemplateDaoClass)beanFactory.getBean("templatedao"); Employee employee=new Employee(); employee.setId(14); employee.setName(" JOSHAF "); employee.setAddress(" USA "); dao.saveEmployeeDetails(employee); } } [/java]
  • Add all the jar files in buildpath to run the program and see the database table.
Table details has to check with the following syntax.
    [sql]select * from employee3;[/sql] Output Output will be as follows.

    Summary

    shape Key Points

    • Spring Hibernate Integration - Spring will have Hibernate Template to provide the less code.
    • Spring Hibernate Integration - Spring will have applicationContext.xml instead of having configuration file.
    • Spring Hibernate Integration - If developer use spring to integrating with hibernate then no need to use Session and committing transaction.