Tuesday, April 26, 2011

JSF 2, Spring 3, JPA (Hibernate 3), PostgreSQL, c3p0 - everything together (part 2 of 2)

Note: below description uses Eclipse Indigo, Tomcat 7.0.28, MyFaces 2.1.7, Spring Framework 3.1.1, Spring Security 3.1.0, Hibernate 3.6.10 (Final), PostgreSQL 9.1.

  • running database and complete project with all required libraries integrated, described in previous post (can be found here)

Step 4 (continued): configuration files - faces-config.xml.

Comparing to a JSF 2.0 project with Spring integrated, only applicationContext.xml file will be changed - web.xml and faces-config.xml files will be the same. We need to add below code into applicationContext.xml:
<bean id="pooledDataSource" 
 class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
 <property name="driverClass" value="org.postgresql.Driver" />
 <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/bikes"/>  
    <property name="user" value="postgres"/>  
    <property name="password" value="pgpass"/> 
    <property name="maxPoolSize" value="10" />
 <property name="maxStatements" value="0" />
 <property name="minPoolSize" value="5" />  
<bean id="JDBCDataSource" 
 class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
 <property name="driverClassName" value="org.postgresql.Driver"/>
 <property name="url" value="jdbc:postgresql://localhost:5432/bikes"/>
 <property name="username" value="postgres"/>
 <property name="password" value="pgpass"/>
<bean id="entityManagerFactory"
 <property name="jpaVendorAdapter">
  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
   Specific properties for Hibernate are in persistence.xml file, 
   but also can be placed here and removed from persistence.xml file.  
 <property name="dataSource" ref="pooledDataSource" />
 <property name="persistenceUnitName" value="persistenceUnit"/> 

 class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean name="transactionManager" 
 <property name="entityManagerFactory" ref="entityManagerFactory" />  

<tx:annotation-driven />
Note that this approach is similar to situation when plain Hibernate API is used with Spring: JPA's EntityManagerFactory is like Hibernate's SessionFactory, JPA's EntityManager (obtained from EntityManagerFactory) is like Hibernate's Session (obtained from SessionFactory). JPA's EntityManager is injected into Repositories (with the help of Spring's PersistenceAnnotationBeanPostProcessor class), while Hibernate's Session was injected into DAO's (with the help of Spring's HibernateDaoSupport class exteneded by each DAO). JPA's TransactionManager (built on EntityManagerFactory) is like Hibernate's HibernateTransactionManager (built on SessionFactory).
JPA's Repositories and Services are similar to oldschool DAOs and Facades. Repositories are injected into Services, while DAOs are injected into Facades.

Please note that I defined here two datasources: a basic one without connection pool (JDBCDataSource) and second one using c3p0 as a connection pool (pooledDataSource).

Step 5: configuration files - persistence.xml

In addition JPA uses a special configuration file named persistence.xml. This file contains additional settings for JPA vendor. The location of this file is described in this article. In order to have this file inside WEB-INF/classes/META-INF/persistence.xml under Eclipse, I created a directory "config" where I put META-INF subdirectory with persistence.xml file inside. Then I select the directory "config" to be used as a source directory. This means that content of "config" directory will be on classpath, and for a web application during a build time it will be moved into WEB-INF/classes. Note that I also added there log4j config file:

Persistence.xml file contains some specific settings for Hibernate (as JPA vendor):
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"

 <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
     <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
     <property name="hibernate.show_sql" value="true" />

Step 6: configuration files - log4j.properties

As I wrote before we will use Log4j instead of Hibernate's default SLF4j. Lets's log everything we can (we can see how Hibernate opens/closes connections, how connections are taken from the pool, how Spring manages transactions and so on):
log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n


Step 7: database entities.

As shown at the beginning of previous post, we had only one entity represented by the class Bike.java. Instances of Bike.java where created inside Spring service named bikeDataProvider (BikeDataProviderImp.java). Now we have a database with tables Bike, Bike_Category, Account and Role. For each table we should create one class (note: this is the simplest mapping one table to one class - there are other way to map tables to classes).

Bike entity:
package com.jsfsample.model;

public class Bike implements Serializable {

 private Integer id;
 private BikeCategory category; 
 public Integer getId() {
  return id;
 public void setId(Integer id) {
  this.id = id;
 public BikeCategory getCategory() {
  return category;
 public void setCategory(BikeCategory category) {
  this.category = category;
 // rest of columns
Bike_category entity:
package com.jsfsample.model;

public class BikeCategory {
 private Integer categoryId;
 public Integer getCategoryId() {
  return categoryId;
 public void setCategoryId(Integer categoryId) {
  this.categoryId = categoryId;
 // rest of columns
Account entity:
package com.jsfsample.model;

public class Person {
 private Integer accountId;
 private Role currentRole;
 public Integer getAccountId() {
  return accountId;
 public void setAccountId(Integer accountId) {
  this.accountId = accountId;
 public Role getCurrentRole() {
  return currentRole;
 public void setCurrentRole(Role currentRole) {
  this.currentRole = currentRole;
 // rest of columns 
Role entity:
package com.jsfsample.model;

public class Role {

 private Integer roleId;
 public Integer getRoleId() {
  return roleId;
 public void setRoleId(Integer roleId) {
  this.roleId = roleId;
 // rest of columns
Note how classes are mapped to tables (using @Table(name="...")) and how fields are mapped to columns (using @Column(name="...")). We also mapped many-to-one relations between tables using @ManyToOne annotation with proper joim column name. Note also how primary keys are generated - we do not use PostgreSQL sequence (GenerationType.SEQUENCE) - PostgreSQL supports SERIAL data type for primary keys, which actually hides sequences (see this link).

Step 8: repositories and services.

Spring repositories are similar to DAO. All database interactions is done insde them. As an example, let's look inside repository resposnible for loading and saving bikes - BikesDAOImpl.java:

package com.jsfsample.repositories.impl;

public class BikesDAOImpl implements BikesDAO {
 private EntityManager em = null;
 public void setEntityManager(EntityManager em) {
  this.em = em;
 public Bike loadSelectedBike(Integer bikeId) {
  return em.find(Bike.class, bikeId);
 // other methods for saving bike and loading bike for given category


This repository is used in Spring's service named bikeDataProvider (BikeDataProviderImpl.java):

package com.jsfsample.services.impl;
public class BikeDataProviderImpl implements BikeDataProvider {

 private BikesDAO bikesRepository;
 private DictionariesDAO dictionaryRepository;

 public Bike getBikeById(Integer id){
  return bikesRepository.loadSelectedBike(id);
 public void add(Bike newBike, Integer categoryId) {
  BikeCategory categorySelected = dictionaryRepository.loadBikeCategroryById(categoryId);
 // other methods using repositories
Similar solution is used for account (person) repository which is then used in Spring's Security service named userDetailsService (UserDetailsServiceImpl.java). Note how easy is to execute a method within database transaction - just use @Transactional annotation and voila.

That's all about adding JPA into Spring-based JSF2 sample application.

The rest of application code (web pages, JSF managed beans) are the same as in pure Spring example project - we only modified services by providing repositories.

How to test it? After deploying application on the server and starting the server, we have to open a browser and type in URL:


Download source files:
Note: make sure that Java, Eclipse and Tomcat are properly installed and configured for running the project (additional configuration may be required if different directories are used).

Eclipse complete sample project is here (with all required libraries). The sample project is a ready to run application which contains JPA (Hibernate), Spring (with Spring Security). You can also download a war file located here (just copy it inside webapps folder in Your Tomcat and start Tomcat with the script startup.bat). Make sure that before doing this You created required database and PostgreSQL server is up and running (You can check access using pgAdmin III).


syed said...

Nice article good for beginners...

but your source code is not downloadable

Anonymous said...

Hey, good job. clear and clean


Anonymous said...

Good evening Pavel,

would it be possible to publish you PostGRe-SQL Script as a MySQL-Script?

Thank you in advance for an answer!

Paweł Nieścioruk said...

I am able to download code using links given in the bottom of this post.
Anyway I will soon move everything to Google Code so it would be easier to download and start everything.

It is quite easy to rewrite this code into MySQL. A little hint: change SERIAL to INT NOT NULL AUTO_INCREMENT and remember about adding TYPE=innodb at the end of each create table statement.
Why do you need this in MySQL?

Anonymous said...

Hello Pawel !
Great tutorial - thx a lot.
About SQL script for Oracle,MySQL etc There is nice property: name= hibernate.hbm2ddl.auto

read more :

Serdecznie pozdrawiam

Rafael said...

Very helpful article! Thank you!

Unknown said...

Thanks for the great article

Unknown said...

Thanks for the great article