JSF 2 Spring Hibernate Example


In this example, we will see how to integrate JSF 2, Spring IoC, Spring Security and Hibernate in a simple web application.

The application will provide : registration page to add new users, login page to perform authentication and welcome page.


1. Technologies used

  • JSF 2.2.8
  • Spring 3.2.8
  • Spring Security 3.2.5
  • Hibernate 4.1.8
  • MySQL 5.1.3
  • Maven 3.0.5
  • Eclipse 4.4
  • JDK 1.7
  • Tomcat 7.0


2. Project structure

JSF 2 Spring Hibernate Example


3. Project dependencies

We need to add dependencies for : JSF 2, Spring Core, Spring Security, Hibernate and JSR-330.

JSR-330 is a Java standard that we will use to perform dependency injection between application layers.


4. Database tables

We will create two tables : USER to store user’s credentials and ROLE to store user’s authorities.


5. Hibernate

We create the model classes to persist the objects related to USER and ROLE tables :

File : src/main/java/com/keylesson/persistence/User.java


File : src/main/java/com/keylesson/persistence/Role.java


Then, we create the mapping files related to User and Role classes :

File : src/main/resources/com/keylesson/persistence/User.hbm.xml


File : src/main/resources/com/keylesson/persistence/Role.hbm.xml


And we add the mapping files in Hibernate configuration file :

File : hibernate.cfg.xml


6. Spring + Hibernate

In the following XML file, we will :
– Activate the auto-scan on com.keylesson package since we will use Spring autowiring instead of XML config
– Add Hibernate SessionFactory to obtain session instance
– Add Hibernate transactionManager bean to manage transaction through the session

File : /WEB-INF/spring-hibernate.xml


In the DAO class, we inject the sessionFactory, and we add method to create new users.

File : UserDAO


While Spring performs the auto-scan, it will load all classes annotated with @Named and consider them as Spring beans (equivalent to @Component).

@Inject is used to perform dependency injection (equivalent to @autowired)


7. JSF2 + Spring

In the managed bean, we inject userManager bean and we create register method.

File : UserController


File : UserModel


File : UserManager


Now, we create the following JSF pages :

Registration page : register.xhtml


Authentication page : login.xhtml


And welcome page : welcome.xhtml


We must add SpringBeanFacesELResolver in JSF file config to make possible the injection of Spring bean into JSF managed bean.

File : /WEB-INF/faces-config.xml


8. Spring Security

Now we will configure Spring Security through spring-security.xml file :

File : /WEB-INF/spring-security.xml


9. Web app configuration

File : web.xml



10. Test It

URL : http://localhost:8080/JSF2-Spring-Hibernate-Example/register.jsf

 JSF 2 Spring Hibernate Example 1


URL : http://localhost:8080/JSF2-Spring-Hibernate-Example/login.jsf

JSF 2 Spring Hibernate Example 2


URL : http://localhost:8080/JSF2-Spring-Hibernate-Example/welcome.jsf

JSF 2 Spring Hibernate Example 3


Download source code


    Salam alikom
    merci pour ce magnifique tuto ,j’aurai cependant une question a vous posez,vous me permettez??

  • Raichand Ray

    It is with eclipse.How to do it with netbeans8.0?Any example or suggestion.

    • Walid RAHALI

      I don’t use netbeans but I suggest you the download the project and import it to netbeans such as I do with eclispe ..or create a maven project directly in netbeans and then copy all project files into this project
      I will soon put a tutorial on how to import maven project to netbeans.

  • Raichand Ray

    Dear Sir,
    Please write a similar kind of CRUD tutorial to search a database using hibernate4 and display search records in primefaces table.
    Each record in has a edit link to display the corresponding record in a edit page and edit page is visible at the time of editing only and a delete button for each record.


  • Gaurav Ghare

    not working..after doing it without maven (with library)also….dont post worst projects and waste our time..

  • Raichand Ray

    Hi Walid,
    Referring your excellent tutorials I have Created a CRUD tutorial using Netbeans

    and a spring security tutorial using Netbeans

    Please look at them.Give good comment if they worth.