Prerequisite Trails
Spring Hibernate Dao Support
Spring Hibernate Integration using SessionFactory
Concept Overview
This tutorial shows how to use annotations to wire a LocalSessionFactoryBean into the SessionFactory object of a DAO. The hibernate session is then obtained from this session factory using the getCurrentSession() method. By default the session uses a SpringSessionContext for the hibernate CurrentSessionContext and a SpringTransactionFactory as the transaction strategy. Please read the tutorials specified in the prerequisite trails to get a comprehensive idea.
Sample Program Overview
The sample program demonstrates spring hibernate integration using annotations..
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 TestSpringHibernateSessionFactoryAnnotation class which will load Spring context and get a reference to PersonService class. We will add a person using the PersonService class and also obtain a list of Persons added.
- cglib.jar
- commons-collections.jar
- commons-logging.jar
- dom4j.jar
- hibernate-annotations-3.4.0.GA.jar
- hibernate-commons-annotations-3.3.0.ga.jar
- hibernate3.jar
- hsqldb.jar
- javaee-api-5.0-2.jar
- jta-1.1.jar
- log4j-1.2.16.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
- slf4j-api-1.6.1.jar
- slf4j-log4j12-1.6.1.jar
Create the Person (see sample code below). This is the entity which is inserted and selected from database using Hibernate.
Hibernate based mapping is done using annotations
@Entity
and
@Table
which tells Hibernate which table to map this entity to (see lines 9-10 below).
The mapping of identifier is done using annotations
@Id
,
@GeneratedValue
and
@Column
(see lines 16-18 below).
This demonstrates the usage of annotations for
Object Relational Mapping
.
Create PersonDao class (see sample code below) which represents the
Data Access Object
which will interact with the database.
This class is annotated using
@Repository
annotation which tells Spring framework to load this class into its container (see line 11 below).
Also note the
@Autowired
annotation which tells Spring to inject
SessionFactoy
into PersonDao (see line 20 below).
The insert() method inserts a Person object into database (see lines 25-31 below). It uses Hibernate’s
SessionFactory
to create the session and finally uses
Session.save()
method to insert the Person object into database (see line 28 below)
Similarly, a list of all Persons is fetched from database using the
selectAll()
method (see lines 33-40 below). It uses Hibernate’s
SessionFactory
to create the session and uses Hibernate’s
Criteria
API to select all persons from database (see line 37 below).
Create PersonService class (see sample code below), a
Service
class which contains reference to PersonDao (see line 9 below). It is annotated using the
@Component
annotation to let Spring load it within its container (see line 8 below).
Note that
@Autowired
annotation is used to inject PersonDao (see line 17 below).
Create addPerson() and fetchAllPersons() methods which delegate the call to PersonDao (see lines 22-28 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 below).
Declare the
context:component-scan
and specify the package from which Spring will load the classes based on their annotations (see lines 13-14 below).
The dependency injection chart for the spring-config is as follows:
Declare the sessionFactory bean (see lines 16-32 below).
Note that
AnnotationSessionFactoryBean
class is used to tell Spring and Hibernate that
Object Relational Mapping
will be done using Annotations (see line 17 below).
The annotated entities used by Hibernate are declared as a property of sessionFactory bean (see lines 20-24 below)
Note how Hibernate specific properties are declared within Spring (see lines 16-32 below).
The database parameters defined for dataSource bean (see lines 34-40 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 (see lines 42-46 below).
Finally, we need a java program to test our setup.This is done by TestSpringHibernateSessionFactoryAnnotation (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 (see line 22 below). We access the list of Persons and print the output to verify that database operations of inserting and selecting using Annotation based mapping within Spring framework have occured successfully (see line 23 below).
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.
(Alternatively you can go the folder containing the springhibernatesessionfactoryannotation-installer.jar and execute the jar using
java -jar springhibernatesessionfactoryannotation-installer.jar
command)
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
springhibernatesessionfactoryannotation
. 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.
Can you provide your pom.xml file