Spring Hibernate Integration Using SessionFactory

Prerequisite Trails

Spring Hibernate Dao Support

Concept Overview

In the previous tutorial we saw how HibernateTemplate can be used in a HibernateSupportDao to perform Hibernate based database operations. However, it is possible to directly obtain a Hibernate session using the getSession() method of the Hibernate SessionFactory. Wire a LocalSessionFactoryBean into the DAO as a SessionFactory object and then use the getSession method of this object to obtain the session. This session is then managed by Spring (The CurrentSessionContext of the hibernate session is set to SpringSessionContext and the transaction strategy is set to SpringTransactionFactory) unless JTA is configured for use.

Sample Program Overview

The sample program demonstrates the use of hibernate session from Spring by wiring a LocalSessionFactoryBean to obtain the SessionFactory. The session is managed by spring.

We will create the Person class with members as id,name and email.

We will create the PersonDao class with members as sessionFactory.

We will create the PersonService class with members as personDao.

We will create the DbUtil class with members as dataSource.

We will also create the spring-config.xml.

Finally, we will test our setup using TestSpringHibernateSessionFactory class which will load Spring context and get a reference to PersonService class. We will add a person using the PersonService class and obtain a list of all Persons added.

Required Libraries
  • cglib.jar
  • commons-collections.jar
  • commons-logging.jar
  • dom4j.jar
  • hibernate3.jar
  • hsqldb.jar
  • jta-1.1.jar
  • log4j.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.beans.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.core.jar
  • org.springframework.expression.jar
  • org.springframework.jdbc.jar
  • org.springframework.orm.jar
  • org.springframework.transaction.jar

Source Code

Create the Person (see sample code below). This is the entity which is inserted and selected from database using Hibernate.

Create Hibernate’s mapping file called Person.hbm.xml (see sample code below).

Map the Person class with the PERSON table (see line 7 below).

Also map the properties Person class with the corresponding columns in PERSON table (see lines 8-12 below).

This mapping file will be used by Hibernate perform Object-Relational Mapping between Person class and PERSON table.

Create PersonDao class (see sample code below) which represents the
Data Access Object
which will interact with the database.

This class contains a reference to Hibernate’s
(see line 11 below).

The insert() method inserts a Person object into database (see lines 20-26 below). It uses Hibernate’s
to create the session and finally uses
method to insert the Person object into database (see line 23 below)

Similarly, a list of all Persons is fetched from database using the
method (see lines 28-35 below). It uses Hibernate’s
to create the session and uses Hibernate’s
API to select all persons from database (see line 32 below).

This class demonstrates the usage of Spring Hibernate integration using

Create PersonService class (see sample code below), a
class which contains reference to PersonDao (see line 7 below).

Create addPerson() and fetchAllPersons() methods which delegate the call to PersonDao (see lines 17-23 below).

Create the DbUtil (see sample code below). This class used only to create the necessary PERSON table in the database.

Create members dataSource (see line 11 below)

Create accessor methods for dataSource (see lines 13-19 below).

Create the initialize method and execute the ‘CREATE TABLE’ statement to create the PERSON table (see lines 21-32)

Create the spring-config.xml file (see XML configuration below).

The dependency injection chart for the spring-config is as follows:

In particular, note how hibernate mapping file is declared within Spring (see lines 27-31 below).

Also note how Hibernate specific properties are declared within Spring (see lines 32-37 below).

The database parameters defined for dataSource bean (see lines 41-47 below) correspond to the HyperSQL in-memory database.

We also declare the dbUtil bean which initializes the database by creating the PERSON table with ID, NAME and EMAIL columns.

Finally, we need a java program to test our setup.This is done by TestSpringHibernateSessionFactory (see source code below).

We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans (see line 13 below).

We get the reference to PersonService class through Spring using the bean name ‘personService’ (see line 14 below).

Create the Person object and set the name and email properties (see lines 16-18 below).

Use the PersonService.addPerson() method to add the Person object (see line 19 below).

Use the PersonService.fetchAllPersons() method to list the Persons added to the database. We access the list of Persons and print the output to verify that database operations of inserting and selecting using Hibernate’s
within Spring framework have occured successfully (see line 23 below).

Running Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run Sample Program
  • Save the springhibernatesessionfactory-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment

  • (Alternatively you can go the folder containing the springhibernatesessionfactory-installer.jar and execute the jar using
    java -jar springhibernatesessionfactory-installer.jar

  • You will see a wizard as shown below
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

    Leave a Comment