Wednesday, October 30, 2013

Step by step configuration of Hibernate, Spring MVC, Primefaces

So, you have decided to use Spring, Hibernate and Primefaces in your application. I am very much fond of each one of them cause, all of them very easy to use and open source. I use all of them in my own application.
But, the first time I tried to configure them in my application, I had nightmare. The most tough part was to configure Hibernate with Spring and to find how to use Spring beans in Managed Beans. But now my application is stable and development has become easy for me now.
So, I thought of sharing the knowledge to configure the application to use all of them.
I will use HSQL as DB for this post. I assume that you are capable of configuring your DB in the configuration files. Because, you are reading this post means, you are an advanced Java Developer.

1.     You have knowledge on Spring, Hibernate and JSF and know the basics of setting them up individually
2.     Knowledge of Eclipse IDE or Maven or any other build tool
3.     Working database
4.     Hibernate 4 and Spring 4 (For another pair of these, generally these configuration needs a little change in the session factory configuration, which you can figure out in ease)
5.     Primefaces (3 or 4) and Spring(3 or 4)
This pair can have any combination. Choose any. Spring versioons are compatible with Primefaces versions.
 I will go step by step for the configurations.

1. Mention the required libraries in your build - Required library list is huge cause Spring, Hibernate and Primefaces depend on different libraries. So please be careful otherwise you will get many exceptions. There are total 28 dependencies in total.
§  antlr-2.7.7.jar
§  commons-logging-1.1.1.jar
§  dom4j-1.6.1.jar
§  el-impl-2.2.1-b05.jar
§  hibernate-commons-annotations-4.0.5.Final.jar
§  hibernate-core-4.3.7.Final.jar
§  hibernate-jpa-2.1-api-1.0.0.Final.jar
§  hsqldb.jar
§  jandex-1.1.0.Final.jar
§  javassist-3.18.1-GA.jar
§  jboss-logging-3.1.3.GA.jar
§  jboss-logging-annotations-1.2.0.Beta1.jar
§  jboss-transaction-api_1.2_spec-1.0.0.Final.jar
§  jsf-api-2.2.8-02.jar
§  jsf-impl-2.2.8-02.jar
§  jstl-1.2.jar
§  primefaces-4.0.jar
§  spring-aop-4.1.3.RELEASE.jar
§  spring-beans-4.1.3.RELEASE.jar
§  spring-context-4.1.3.RELEASE.jar
§  spring-context-support-4.1.3.RELEASE.jar
§  spring-core-4.1.3.RELEASE.jar
§  spring-expression-4.1.3.RELEASE.jar
§  spring-jdbc-4.1.3.RELEASE.jar
§  spring-orm-4.1.3.RELEASE.jar
§  spring-tx-4.1.3.RELEASE.jar
§  spring-web-4.1.3.RELEASE.jar
§  spring-webmvc-4.1.3.RELEASE.jarWith all these dependencies, WebContent/WEB-INF/lib looks like

2.       Setup JSF for your application
Dependency declaration is complete. The next step is to setup Faces for your application.
In your web.xml mention faces servlet and the extensions you want to map with JSF and run your server. It should be working.
If not, then delete the web.xml and retry. Here is one sample for ease.

 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi=""  
      xmlns="" xmlns:web=""  
      id="WebApp_ID" version="2.5">  
      <display-name>PrimeFaces Web Application</display-name>  
      <!-- Change to "Production" when you are ready to deploy -->  
      <!-- Welcome page -->  
      <!-- JSF mapping -->  
           <servlet-name>Faces Servlet</servlet-name>  
      <!-- Map these files with JSF -->  
           <servlet-name>Faces Servlet</servlet-name>  
           <servlet-name>Faces Servlet</servlet-name>  
           <servlet-name>Faces Servlet</servlet-name>  
           <servlet-name>Faces Servlet</servlet-name>  

3.       Add Spring MVC to Faces
Now, add Spring MVC to your faces application. You have to include  Spring context file as context-param with param name contextConfigLocation and RequestContextListener and ContextLoaderListener as your application listener.
So, basically you have to add three more tags in your web.xml.
Now, it’s time to write faces-config.xml. Here you mention SpringBeanFacesELResolver as the EL Resolver for your faces application.
After adding these, restart the server to check if configuration is working.


This is the faces-config file
 <?xml version="1.0" encoding="utf-8"?>  
 <faces-config xmlns=""  

4.       Add Hibernate to Spring
In your context file, DriverManagerDataSource as data source, hibernate4.LocalSessionFactoryBean as sessionFactory and use this in your DAO classes. Add POJO, hibernate configurations and mappings etc in the project.
Again restart the server and it is in complete configuration.

 <beans xmlns:xsi="" xmlns="" xsi:schemalocation="">  
      <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">  
           <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver">  
           <property name="url" value="jdbc:hsqldb:hsql://localhost:1521/opt/db">  
           <property name="username" value="SA">  
           <property name="password" value="">  
      <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">  
           <property name="dataSource" ref="dataSource">  
           <property name="configLocation" value="classpath:hibernate.cfg.xml">  
      <bean class="dao.EmployeeDaoImpl" id="employeeDAO">  
           <property name="sessionFactory" ref="sessionFactory">  
      <bean class="service.EmployeeService" id="service">  
           <property name="dao" ref="employeeDAO">  

Hibernate configuration
 <?xml version="1.0" encoding="utf-8"?>  
 <!DOCTYPE hibernate-configuration SYSTEM   
           <property name="hibernate.dialect">  
           <property name="hibernate.connection.driver_class">  
           <!-- Assume test is the database name -->  
           <property name="hibernate.connection.url">  
           <property name="hibernate.connection.username">  
           <property name="hibernate.connection.password">  
           <!-- List of XML mapping files -->  
           <mapping resource="resources/Employee.hbm.xml" />  

 <?xml version="1.0" encoding="utf-8"?>  
 <!DOCTYPE hibernate-mapping PUBLIC   
  "-//Hibernate/Hibernate Mapping DTD//EN"  
   <class name="bean.Employee" table="EMPLOYEE">  
    <meta attribute="class-description">  
      This class contains the employee detail.   
    <id name="sapCode" type="int" column="sap_code">  
      <generator class="native"/>  
    <property name="firstName" column="first_name" type="string"/>  
    <property name="lastName" column="last_name" type="string"/>  
    <property name="middleName" column="middle_name" type="string"/>  

5.   Retrieve Spring beans in Managed Beans
All the setup is complete. To retrieve Spring beans, in your Managed beans, use @ManagedProperty on attributes.

 package managedbeans;  
 import javax.faces.bean.ManagedBean;  
 import javax.faces.bean.ManagedProperty;  
 import service.EmployeeService;  
 @ManagedBean(name = "editor")  
 public class EditorBean {  
      @ManagedProperty(name = "service", value = "#{service}")  
      private EmployeeService service;  
      private String value = "Editor";  
       * @return the value  
      public String getValue() {  
           return value;  
       * @param value  
       *      the value to set  
      public void setValue(String value) {  
           this.value = value;  
       * @return the service  
      public EmployeeService getService() {  
           return service;  
       * @param service  
       *      the service to set  
      public void setService(EmployeeService service) {  
           this.service = service;  
      public void save() {  
           System.out.println("Using service");  

Enjoy the development with these components. They are really cool stuffs.

I've complete websites built on top of these three only. They really are helpful for faster development, better maintainability, less coding, good looking GUI. You are free to take a look in the below locations,

That's it. If anything breaks, in between, you can post comment, I will check and revert back.

If this helped you out, you can share this in your network as well. Sharing is caring...

Palash Kanti Kundu