PrimeFaces 5 + Spring Data + MySQL Integration

This tutorial guides you through creating a web application that integrates between PrimeFaces 5, Spring Data and MySQL database. It’s important for you to learn the basics of the technologies PrimeFaces and Spring Data before start reading this tutorial.

Also read:


1. Tools Required

It’s the required tools for being able to develop this tutorial:

  • JDK 1.6+.
  • Tomcat 7+.
  • Spring 3.
  • Primefaces 5.
  • MySQL 5.

2. Java Beans (Business Domain)

Beans Address and Employees will be used as the entities for this tutorial.


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity(name = "address")
public class Address {
	private Integer addressId;
	private String addressCountry = "";
	private String addressCity = "";

	@OneToOne(cascade = CascadeType.ALL, mappedBy = "address")
	private Employee employee;

	public Employee getEmployee() {
		return employee;

	public void setEmployee(Employee employee) {
		this.employee = employee;

	public Integer getAddressId() {
		return addressId;

	public void setAddressId(Integer addressId) {
		this.addressId = addressId;

	public String getAddressCountry() {
		return addressCountry;

	public void setAddressCountry(String addressCountry) {
		this.addressCountry = addressCountry;

	public String getAddressCity() {
		return addressCity;

	public void setAddressCity(String addressCity) {
		this.addressCity = addressCity;


import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

public class Employee {
	private Integer employeeId;

	@Basic(optional = false)
	private String employeeName;

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "Address")
	private Address address = new Address();

	public Address getAddress() {
		return address;

	public void setAddress(Address address) {
		this.address = address;

	public Integer getEmployeeId() {
		return employeeId;

	public void setEmployeeId(Integer employeeId) {
		this.employeeId = employeeId;

	public String getEmployeeName() {
		return employeeName;

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;

3. Persistence Context

Here is the required persistence configuration, which contains the targeted database schema and its credential details and any additional configuration required like logging level and etc. Also this persistence configuration declare the above listed beans as the persistence entities.


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
	xmlns="" xmlns:xsi=""
	<persistence-unit name="SpringData"
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JavaBeat" />
			<property name="javax.persistence.jdbc.user" value="root" />
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
			<property name="javax.persistence.jdbc.password" value="root" />
			<property name="eclipselink.logging.level" value="OFF" />

4. Spring Data Repositories

Here is the spring data repository for each of the above entities. If you are familiar with the spring data repositories, these are just an abstract behaviour where the real implementation would be provided by the spring container at run time. It is the greatest advantage of using the spring data.

package net.javabeat.springdata.repo;

import org.springframework.stereotype.Repository;

public interface AddressRepository extends CrudRepository<Address,Integer>{}

package net.javabeat.springdata.repo;

import org.springframework.stereotype.Repository;

public interface EmployeeRepository extends CrudRepository<Employee,Integer>{}

5. Spring Service

It’s just a spring bean that used for preventing the presentation layer talking directly with the repository. It’s just a type of orchestration that you would use. Spring bean will contain an instance of those defined repositories.

package net.javabeat.springdata.beans;

import net.javabeat.springdata.repo.EmployeeRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class RegistrationService {
	private EmployeeRepository employeeRepository;

	public EmployeeRepository getEmployeeRepository() {
		return employeeRepository;

	public void setEmployeeRepository(EmployeeRepository employeeRepository) {
		this.employeeRepository = employeeRepository;

6. Spring Context Configurations

It’s the minimal amount of lines that should be updated in the spring context, from which the Spring Container could initialize the beans and any other environment information needed for the spring container.


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""
	xmlns:xsi="" xmlns:tx=""
	xmlns:aop="" xmlns:util=""

	<!-- For consider the using of annotations foe defining Spring Bean -->
	<context:annotation-config />

	<!-- For defining Spring Bean -->
	<context:component-scan base-package="net.javabeat.springdata.beans" />

	<!-- For bootstrapping the Spring Repository -->
	<jpa:repositories base-package="net.javabeat.springdata.repo" />

	<!-- Necessary to get the entity manager injected into the factory bean -->
		class="" />

	<!-- Define EclipseLink JPA Vendor Adapter -->
	<bean id="jpaVendorAdapter"
		<property name="databasePlatform"
			value="org.eclipse.persistence.platform.database.MySQLPlatform" />
		<property name="generateDdl" value="false" />
		<property name="showSql" value="true" />

	<!-- Entity Manager Factory -->
	<bean id="entityManagerFactory"
		<property name="persistenceUnitName" value="SpringData"></property>
		<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />

	<!-- Transaction Manager -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />

	<!-- Enable Transactional Manner -->
	<tx:annotation-driven transaction-manager="transactionManager" />


7. Primefaces / JSF Faces Configuration

It’s the faces configuration XML file, defines the managed beans and spring expression resolver.


<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns=""

8. Primefaces / JSF Managed Bean

It’s the user defined managed bean that contain the business logic of the registration business scenario used in this tutorial.

package net.javabeat.primefaces.managedbeans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import net.javabeat.springdata.beans.RegistrationService;


public class RegistrationManagedBean {

	private Employee employee = new Employee();

	private List<Employee> employees = new ArrayList<Employee>();

	private RegistrationService service;

	public Employee getEmployee() {
		return employee;

	public void setEmployee(Employee employee) {
		this.employee = employee;

	public List<Employee> getEmployees() {
		this.employees = Lists.newArrayList(this.service.getEmployeeRepository().findAll());
		return employees;

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;

	public RegistrationService getService() {
		return service;

	public void setService(RegistrationService service) {
		this.service = service;

	public String register(){
		this.employee = new Employee();
		return "";

9. Primefaces View

It’s the view that will be used for presenting the required primefaces UI components for Employee & Address registration business scenario.


<html xmlns=""
		<script name="jquery/jquery.js" library="primefaces"></script>
		<h:form prependId="false">
			<h2>JavaBeat Tutorials</h2>
			<h2>Primefaces + Spring Data + MySQL</h2>
			<h:panelGrid columns="2">
				<h:outputText value="Enter Employee Name:"/>
				<p:inputText value="#{registrationManagedBean.employee.employeeName}"></p:inputText>
				<h:outputText value="Enter Employee Address Country:"/>
				<p:inputText value="#{registrationManagedBean.employee.address.addressCountry}"></p:inputText>
				<h:outputText value="Enter Employee Address City:"/>
				<p:inputText value="#{registrationManagedBean.employee.address.addressCity}"></p:inputText>
			<p:commandButton value="Register" action="#{registrationManagedBean.register}" ajax="false"/>
			<h:panelGrid columns="1" width="50%">
				<p:dataTable value="#{registrationManagedBean.employees}" var="employee">
					<p:column headerText="Employee's Name">
						<h:outputText value="#{employee.employeeName}"/>
					<p:column headerText="Employee's Country">
						<h:outputText value="#{employee.address.addressCountry}"/>
					<p:column headerText="Employee's City">
						<h:outputText value="#{employee.address.addressCity}"/>

10. Web Deployment Descriptor

It’s the required XML file that used by the Java EE container for understanding of the web application that will be getting deployed.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" metadata-complete="true" version="2.5">
    <description>State saving method: 'client' or 'server'
						(=default). See JSF Specification 2.5.2
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-name>Faces Servlet</servlet-name>

11. PrimeFaces 5 + Spring Data + MySQL Demo

Primefaces Spring Data Registration View
Primefaces Spring Data Registered Employee

12. Database Records

Primefaces Registered Employees

13. Summary

Finally, we have  implemented a web application that uses the PrimeFaces 5, Spring Data & MySQL. If you have any questions, please write it in the comments section.

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!