Spring Hibernate Integration

In this post we will be discussing on Spring Hibernate integration. As we all know Spring provides an ORM support and one of the best available ORM tool to be integrated with Spring is Hibernate. The integration is very much simple and comes with lot of flexibility which is the biggest advantage Spring comes with. Lest create a step wise project for simple application.

Project Structure

Create a Dynamic Web Project in your eclipse, lets say “Spring-Hibernate-Integration”. Add the required jar files for Spring and Hibernate and add the runtime environment, in this case we are adding Apache Tomcat 7. At the end our project structure and the required dependencies in the lib folder will be as shown below.

spring-hibernate-project-structure
spring-hibernate-project-structure

The libraries to be added –

spring-hibernate-jars
spring-hibernate-jars

Configuration (XML and Properties)

  1. WEB.XML file – It contains simple mapping of dispatcher servlet along with the welcome page to load.

2. dispatcher-servlet.xml – It contains the details of Spring and hibernate beans to be used along with the configurations.

In this configuration file, we have created a bean for data source, session factory and transaction manager. All these three beans will be used for all the transactions with the data base. Data source bean will give the details about the data base and will give the connection object. Session factory will create the session from the given data source so that transaction can be done using transaction manager with the data base. Transaction manager will make sure that all the transactions are safe and will take care of clearing and maintaining sessions in the server. While creating the session factory we have mentioned a bean that will be mapped to the data base table “users”. Please note, you can also configure such annotated beans apart from this xml file that is in your java files.

3. jdbc.properties file : This file contains the details of database like, driver, port, username, password, url etc. This file will be loaded in the dispatcher servlet to read the details and create a datasource.

View Pages:

Lets create a JSP page which contains a form that will submit the data to the server to be stored in data base. Along with this, it will load all the user available in the database along with the current user submitted.

Controllers, Services and Beans

Lets create a controller class for mapping the requests.

The controller class requires a UserService to process the request and a User model to process on. Lets create a user service and a user DAO which will process user bean that is be mapped to the data base table “users”.

User Service Interface and implementation:

User Service Implementation :

User bean that consists of the mapping of each row to a single property of this POJO class. @Entity and @Table(name = “users”) annotations are used in the below bean class to map the bean with the “users” table in the data base. Each column is mapped to one of the columns in the table using @Column annotation.

UserDAO class to process the transactions:

Output

And we are done with the coding part now. Just execute your application on the server and see the results. In this case the output will be-

First page :

spring-hibernate-page
spring-hibernate-page

Initial table details before any submission :

spring-hibernate-table
spring-hibernate-table

Data submission :

spring-hibernate-data
spring-hibernate-data

Data submission response:

spring-hibernate-data-insert
spring-hibernate-data-insert

Table content after the data submission :

spring-hibernate-inserted-data
spring-hibernate-inserted-data

Hope this helps you.

Regards,

Nikhil Naoghare.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.