Primefaces, Spring 4 with JPA (Hibernate 4/EclipseLink) Example Tutorial

Java Persistence API is a standard specification. It provides a persistence model that’s implemented by different implementer frameworks.

Primefaces Spring Hibernate EclipseLink

Hibernate & EclipseLink are two most popular implementations used for persisting given business model against some sort of persistence store like relational database. As such, this tutorial will provide you a full-fledged example containing all required configuration steps to developer a layered application that uses:

  1. Primefaces components to develop a compelling User Interface that aimed to handle user’s interactions and verify user’s inputs.
  2. Hibernate/EclipseLink implementations to develop an Object/Relational Mapping beneath JPA umbrella.
  3. Spring framework as a kind of glue that get everything attached each together.

We’ve discussed before using Hibernate ORM for persisting given domain classes. But today we will use only JPA based configurations.

JPA specification does its bootstrap in a different way. In hibernate we’ve bootstrapped our application using hibernate.cfg.xml file, but JPA doesn’t specify that file.

JPA provides another way of configuration, it’s using persistence.xml file that is located within your classpath and under META-INF folder.

Let’s see how can we use both of Hibernate and EclipseLink for implementing a single registration form.

Primefaces Spring JPA Hibernate EclipseLink Example Required Tools

Before proceeding far away, you must prepare your environments that should contain for:

  • JDK 1.6+.
  • Eclipse Kepler 4.3.
  • Hibernate 4.3.6.Final.
  • Spring 4.0.3.RELEASE.
  • EclipseLink 2.5.0-RC1
  • Maven Build Tool
  • MySQL 5.x.

Primefaces Spring JPA Hibernate EclipseLink Example Project Structure

Primefaces Spring JPA Hibernate EclipseLink Example Database Tables

We have Employee table in our MySQL database, you can use below script to create it.

Database-Tables-Create-Employee-Table

  • Employee Table contains one Primary Key with Auto Increment value.

Primefaces Spring JPA Hibernate EclipseLink Example Domain Classes

We have also one domain class that would be persisting into our database Employee table.

Employee.java

  • JPA provides @Entity which will be used for indicating Employee as a persistent domain class. Default mapping would be happening in order to map this persistent entity with its Employee Table. In case you’ve provided Table name or class name that aren’t identical, @Table must be used.
  • @Id annotation used for indicating identity of a given Employee instance. Because of discrepancies between attribute name and column name, @column must be provided.
  • @Column name annotation takes a parameter of mapped column name.

Primefaces Spring JPA Hibernate EclipseLink Example Persistence Unit

As we’ve mentioned earlier, JPA provides an alternative way for bootstrapping JPA framework, it’s a persistence.xml file. The minimum amount of this file should look like:

persistence.xml

Persistence unit should define:

  • Persistence unit name. That name will be referenced by Spring context.
  • Transaction type – JPA implementation have the choice of managing the resource by itself (RESOURCE_LOCAL) or having them managed by the application server’s JTA implementation.
  • Information about database connection.

Primefaces Spring JPA Hibernate EclipseLink Example Maven Dependencies

All required libraries are listed within pom.xml file that’s read by Maven itself.

pom.xml

Hibernate/JPA Spring Configuration

Persisting using of JPA requires an instance of EntityManager. This instance can be acquired by configuring a proper Spring context.

applicationContext.xml

  1. JPA require an entityManagerFactory object which is an instance of org.springframework.orm.jpa.LocalEntityFactoryBean. This instance must be provided with the name of persistenceUnit and a JPAVendorAdapter.
  2. To use @Trasnactional annotation properly, TransactionManager should be defined.
  3. Default name and location for Spring context configuration is applicationContext.xml and beneath of WEB-INF folder.

EclipseLink/JPA Spring Configuration

Same configuration would be used for EclipseLink, a small change is required is to provide EclipseLink’s JPA vendor. Just change the jpaVendorAdapter bean to below and the JPA implementation used will be EclipseLink.

applicationContext.xml

Primefaces Deployment Descriptor

Proper configuration of Spring requires adding of Spring listener into Primefaces’ deployment descriptor web.xml application.

web.xml

Spring EmployeeService

Spring service is the interaction point between presentation layer and persistence layer. If you’re familiar with DAO, you can consider it something similar.

EmployeeService.java

  1. EntityManager is injected using @PersistenceContext annotation. Even you’ve defined an instance of EntityManagerFactory, but a JPA implementation will be very smart to inject you an instance of EntityManager. EntityManager would be something similar for Session in Hibernate. In case you’ve invoked any of its CRUD operation within both of context and active transaction, your operation would be persisted against your persistence store. Note that em.persist() and using of @Transactional annotation upon register method.

Primefaces Managed Bean – RegisterEmployee

RegisterEmployee is a faces managed bean that’s used for handling user interaction and validation of user’s input.

ResgiterEmployee.java

  • Spring service EmployeeService is injected using Spring el-reslover that get declared with your faces-config.xml.
  • Register method would delegate the invocation into an injected EmployeeService instance. As such, EmployeeService would handle real registration.

Primefaces Employee Registration Page

index.xhtml

Employee-Registered-1024x181

Employee-Persisted

Primefaces Spring JPA Hibernate EclipseLink Example Summary

This tutorial aimed to help you get both of Hibernate and EclipseLink JPA implementations used into your project. JPA has changed your life, it’s so easy to configure, use and track. It’s plugged in with a default logging mechanism that would help you find your problem shortly. Contribute us by commenting below and find downloaded source code.

By admin

Leave a Reply

%d bloggers like this: