JSF Spring Hibernate Integration Example Tutorial

Welcome to JSF Spring Hibernate Integration example tutorial. In our last tutorial, we saw how to integrate JSF and Spring frameworks together. Today we will learn how to integrate JSF Spring Hibernate frameworks. This is a favorable combination where we are using the best choice for user interface, server side dependency injection and ORM tool.

JSF Spring Hibernate

Our goal is to create a single page user interface from which we can enter some data that goes into database. We get the same response page where we also show the data from the table, so newly added data should also be there on page submit.

Below image shows our final spring jsf hibernate integration project structure. Let’s start now with the project and see how each of the frameworks are glued together to achieve our goal.

JSF-Login-Logout-Authentication-Project

JSF Spring Hibernate Project Setup

Create a Dynamic Web Project in Eclipse and then convert it to Maven project, so that we have basic setup ready. Now we must add required dependencies in our pom.xml file. Below is our final pom.xml file. Notice the JSF, Spring, Spring ORM and Hibernate dependencies. We are using MySQL database for our example and Apache DBCP for creating simple database connection pool.

Now use below script to create the required table in database.

Next we need is to add Spring Listener and JSF Controller Servlet in the web.xml file as shown below.

Next we need JSF to integrate with Spring framework, for that we need to configure SpringBeanFacesELResolver in faces-config.xml file. It will make sure that JSF view pages variables are mapped to the backend managed beans.

faces-config.xml

The last configuration we need is the spring beans configuration but we will look at that after going through the java classes.

JSF Spring Hibernate Model Classes

We have Person.java class that is mapped with the Person table in database. Notice the use of JPA and JSF annotations.

Now we will move over to creating DAO classes to interact with the database tables.

JSF Spring Hibernate DAO Classes

PersonDAO.java

We have two method, one to insert data into database table and another one to fetch the list of Persons to display in user interface.

Below is the implementation class for above DAO.

PersonDAOImpl.java

Now we will create service classes and then do the wiring.

JSF Spring Hibernate Service Classes

PersonService.java

PersonServiceImpl.java

Notice the use of @ManagedBean JSF annotation and @Transactional annotation for transaction management.

Recommended Read: Spring Hibernate Integration and Spring Transaction Management

Spring Beans Configuration File

Our Spring Hibernate based backend services are ready, now we can wire them in the spring beans configuration file as shown below.

applicationContext.xml

Notice the dataSource configuration, you will have to adjust them according to your setup.

JSF View Page

The last part is to write JSF view page and use the managed beans as configured above.

person.xhtml

That’s it, now just deploy the application in your favorite servlet container and run it, you should get below page.

JSF-Spring-Hibernate-Example-366x450

I already have some entries in the Person table and that is shown in the list above, for the first time you won’t see the list.

As always, you can download the project from below link and play around with it to learn more.

By admin

Leave a Reply