How to configure Hibernate in Spring.

The objective of the tutorial is to understand how the DB can be configured in Spring using Hibernate.
To start with download:
1) spring jars
2) hibernate jars
3) mysql driver

Following is the list of jars I used.

1) create a Db in mysql with name “reco_engine”;
2) Create a table User and add some columns to it.
3) Insert some records into the table using mysql..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="hibernateDao" class="HibernateAccessDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
<bean id="dataSource" 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/reco_engine"/>
<property name="username" value="root"/>
<property name="password" value="root"/>

<bean id="mySessionFactory"
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<property name="hibernateProperties">

<bean id="hibernateTemplate"
<property name="sessionFactory">
<ref bean="mySessionFactory"/>

The bean “datasource” is used to configure the properties of the DB.
The bean “mySessionFactory” is used to maintain the session of hibernate. This is taken care by the spring container now.
The property “mappingResources” takes the list of hbm files which will be mapped to this session. Also the datasource property is set in this bean.

The bean hibernateTemplate is the one used to interact with the DB. The Spring application context will manage its lifecycle, initializing and shutting down the factory as part of the application. LocalSessionFactoryBean is the preferred way of obtaining a reference to a specific Hibernate SessionFactory, at least in a non-EJB environment.

The bean “hibernateDao” is the java class and the hibernateTemplate property is set.

The Users table has to be mapped in Hibernate. To map it , and Users.hbm.xml need to be created . I have used the Hibernate plugin for eclipse provided by JBoss to generate the files automatically.

The files will be as follows:

public class Users  implements {

    // Fields    

     private int id;
     private String firstname;
     private String lastname;
    // Constructors

    /** default constructor */
    public Users() {

	/** minimal constructor */
    public Users(int id) { = id;
    // Property accessors

    public int getId() {
    public void setId(int id) { = id;

      public String getFirstname() {
        return this.firstname;
    public void setFirstname(String firstname) {
        this.firstname = firstname;

    public String getLastname() {
        return this.lastname;
    public void setLastname(String lastname) {
        this.lastname = lastname;

The Users.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<!-- Generated Jun 3, 2010 9:15:59 PM by Hibernate Tools 3.1.0.beta4 -->
    <class name="Users" table="users" catalog="reco_engine">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        <property name="firstname" type="string">
            <column name="firstname" length="45" />
        <property name="lastname" type="string">
            <column name="lastname" length="45" />
        <property name="designation" type="string">
            <column name="designation" length="45" />
        <property name="department" type="string">
            <column name="department" length="45" />

The Dao class to interact with the database:

public class HibernateAccessDao {

	HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext context =   new ClassPathXmlApplicationContext("spring.xml");
		HibernateAccessDao dao =(HibernateAccessDao) context.getBean("hibernateDao");
// This method retrieves the record with id =1
	private void getRecords(){
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Users user = (Users) session.load(Users.class, 1);


The session.load(User.class,1) will load the record of the user with id =1