Friday, 4 August 2017

Spring4,Primefaces5,SpringDataJPA and Hibernate4 CRUD Application Using Netbeans8.02 IDE and MySQL Database Server

This simple application is a Create, Retrieve, Update and Delete (CRUD) application operating on an ‘employee’ table in ‘springdatacruddb’ database in MySQL Database Server. It is a Primefaces, Spring, SpringDataJPA and Hibernate-annotation based application.
Software Used
2.Netbeans 8.02
3.MySQL 5.* Database Server(or XAMPP-For easy MySQL Management)
4.MySQL Connector 5.*
5.Hibernate 4.3.** and Primefaces 5.0(Bundled with Netbeans)
Eclipse can also be used for developing this application.
For Integrating Maven, pom.xml is provided at the end of this tutorial.
1.Install JDK8 or Jdk7 if not installed
2.Install Netbeans and associated ApacheTomcat Server
3.Install  MySQL Database server or XAMPP(For easy management of MySQL ) .
Create springdatacruddb  database in MySQL Database Server.

After Installing Netbeans click the services tab on the left.Expand Database node. Expand Drivers node. Right click MySQL(Connector/Jdriver) then connect. Put springdatacruddb as the database. As shown below. Put password if you have given password at the time of installation of MySQL database server. For XAMPP no password is required. Then test connection. If successful click finish button.

Create user table by running below SQL in ‘springdatacruddb’ database
  `EmpId` int(15) NOT NULL,
  `FirstName` varchar(25) NOT NULL,
  `LastName` varchar(25) NOT NULL,
  `Salary` int(20) NOT NULL,
  `Sex` varchar(15) NOT NULL,
  `DOB` date NOT NULL,
  `Remark` varchar(50) NOT NULL
ALTER TABLE `employee`   ADD PRIMARY KEY (`EmpId`);

INSERT INTO `employee` (`EmpId`, `FirstName`, `LastName`, `Salary`, `Sex`, `DOB`, `Remark`) VALUES
(1, 'Andrei', 'Agassi', 2000000, 'Male', '1980-06-18', 'Very Good Tennis Player'),
(2, 'Martina', 'Navratilova', 60000000, 'Female', '1978-12-15', 'Very Good Tennis Player'),
(3, 'Venus', 'Williams', 7000000, 'Female', '1975-12-21', 'Very Goog Tennis Player'),
(4, 'Roger', 'Fedrer', 600000, 'Male', '1987-12-20', 'Very Good Tennis Player'),
(5, 'Lindsay', 'Devonport', 600000, 'Female', '1974-12-14', 'Very good  Tennis Player '),
(6, 'Serena', 'Williams', 600000, 'Female', '1988-12-16', 'Very Good Tennis Player'),
(7, 'Barak', 'Obama', 5463216, 'Male', '1961-12-18', 'Ex-President USA'),
(8, 'Mary', 'Pierce', 8654321, 'Female', '1983-06-14', 'Very good beautiful Tennis Player'),
(9, 'Rafel', 'Nadal', 754321, 'Male', '1991-04-12', 'Very Goog Tennis Player'),
(10, 'Sir Donald', 'Bradman', 650000, 'Male', '1942-05-06', 'Very Good Cricket Bowler'),
(11, 'Usin', 'Bolt', 690700, 'Male', '1998-12-16', 'Olympics  Gold Medalist in Athletics'),
(12, 'Micheal', 'Phelps', 765000, 'Male', '1992-12-22', 'Olympics  Gold Medalist in Swimming.USA Citizen.'),
(13, 'Andy', 'Moore', 750000, 'Male', '1991-05-09', 'Very Goog Tennis Player'),
(14, 'Tom', 'Cruise', 7000000, 'Male', '1975-01-15', 'Hollywood Actor'),
(15, 'Maria', 'Sharpova', 8000000, 'Female', '1980-04-06', 'Very Good Beautiful Tennis Player'),
(16, 'Martina', 'Hingis', 850000, 'Female', '1982-02-18', 'Very Goog Tennis Player'),
(17, 'Vin', 'Diesel', 840000, 'Male', '1985-01-17', 'Very Good Actor'),
(18, 'Angelina', 'Jolie', 7500000, 'Female', '1987-11-14', 'Very Good Actress'),
(19, 'Brad', 'Pitt', 765000, 'Male', '1979-01-24', 'Very Good Actor'),
(20, 'Steffi', 'Graff', 780000, 'Female', '1984-12-10', 'Very Good Beautiful Tennis Player');

JARS required to be added to Libraries Folder
Right click on the Libraries folderà addJAR/Folder then add below mentioned JAR Files .
5. Jandex-2.0.3
6. spring-aop-4.3.2
7. spring-beans-4.3.2
8. spring-context-4.3.2
9. spring-core-4.3.2
10. spring-tx-4.3.2
11. spring-web-4.3.2
12. spring-expression-4.3.2
13. spring-jdbc-4.3.2
14. spring-orm-4.3.2
15. Spring-data-commons-1.12.5
16. Spring-data-jpa-1.10.5
Creating Project MavenPrimefacesSpringDataJPACRUD
File-àNew ProjectàCategories-àChoose JavaWeb--àChoose WebApplicationàClick Next-àGive Project Name MavenPrimefacesSpringDataJPACRUDà
Click NextàClick NextàChoose Framework First Hibernate then Java Server Faces--àClick Component Tab-àChoose Primefacesà Click Finish

In the above figure Database Connection should be with springdatacruddb’ database.

In the above figure Database Connection should be with springdatacruddb’ database

Download mysql- connector-java-bin.jar add to libraries folder by right click addJAR/Folderàadd the mysql-java-bin.jar.
Create a folder named View under Web pages Folder.Delete WelcomePrimefaces.xhtml .View Folder would contain crud.xhtml File  

Project Structure

Creating Packages and Classes
Right click Source Package folder and create five packages>This would contain JSF Managed Bean Class
2. org.ray.springdataJpa.dao.repositories-àThis would contain DAO(Data Access Object) RepositoriesàThis would contain one file
4. org.ray.hibernate.pojos.modelàThis would contain entity (POJO) class  files POJO Stands for Plain Old Java Objects
5. org.ray.spring.serviceàThis would contain Spring Service class files and EmployeeServiceImpl.Java.

Following Files would be created  using Netbeans
1. hibernate.cfg.xml File-àAutomatically generated. (It will be used to create and then would be deleted.)
2. Reverse Engineering File-àhibernate.reveng.xml. (It will be used to create  and then would be deleted.)
3.Entity(POJO) File-à (POJO stands for Plain Old Java Objects)
4.JSF Managed Bean File-àEmployeeBean.ja va
5.Employee Repository Fileà
6.Exception handling Fileà
7.SpringService FileàEmployeeService .java and
8. crud.xhtmlàThis is single page which is displayed on lunch of application.
9.faces-config.xmlàIt is to be added after creating under WEB-INF folder if not there.
10. web.xml (Automatically generated)
11. spring-database.xmlàThis contains spring file database access credentials.
Add mysql- connector-java-bin.jar to libraries if not done.
1.Hibernate.cfg.xml  File(It would be deleted after used for creating   entity class)
As XAMPP is used so there is no password in the file only username is given that is root in Hibernate.cfg.xml File.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "">
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ springdatacruddb?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"> </property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="">update</property>
    <mapping class="org.ray.hibernate.pojos.model.Employee"/>
2. Creating Reverse Engineering File-àhibernate.reveng.xml(It would be deleted after used for creating  entity class).
Right Click default package in the Source Package-ànewàchoose Hibernate Reverse Engineering Wizardàclick nextàchoose employee tableàAdd àclick finish.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "">
  <schema-selection match-catalog=" springdatacruddb "/>
<table-filter match-name="employee"/>
3. Creating Hibernate  Entity (pojo)
Important:To create this file MySQL database springdatacruddb most be connected through Netbeans.
Right click com.model package--ànew-àHibernate Mappling Files and pojos from databaseàDonot select mapping file,select EJB3.0 Pattern and Domain Code(java) and also select JDK5  Language Features(Though not shown below)àCliek Finish CODE:
package org.ray.hibernate.pojos.model;
// Generated Nov 30, 2016 11:34:46 AM by Hibernate Tools 4.3.1

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

 * @author Raichand
public class Employee  implements {

     private int empId;
     private String firstName;
     private String lastName;
     private int salary;
     private String sex;
     private Date dob;
     private String remark;

    @Column(name="EmpId", unique=true, nullable=false)
    public int getEmpId() {
        return this.empId;
    public void setEmpId(int empId) {
        this.empId = empId;
    @Column(name="FirstName", nullable=false, length=25)
    public String getFirstName() {
        return this.firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    @Column(name="LastName", nullable=false, length=25)
    public String getLastName() {
        return this.lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;
    @Column(name="Salary", nullable=false)
    public int getSalary() {
        return this.salary;
    public void setSalary(int salary) {
        this.salary = salary;
    @Column(name="Sex", nullable=false, length=15)
    public String getSex() {
    public void setSex(String sex) { = sex;
    @Column(name="DOB", nullable=false, length=10)
    public Date getDob() {
        return this.dob;
    public void setDob(Date dob) {
        this.dob = dob;

    @Column(name="Remark", nullable=false, length=50)
    public String getRemark() {
        return this.remark;
    public void setRemark(String remark) {
        this.remark = remark;

public void reset() {
    //This method writes the values of employee object with System.out.println(employee.toString()) code
    public String toString() {
    return "employee"
    + "\n\t Id: " + this.empId  
    + "\n\t FirstName: " + this.firstName      
    + "\n\t LastNmae: " + this.lastName
    + "\n\t Sex: " +    
    + "\n\t Salary: " + this.salary       
    + "\n\t Date of Birth: " + this.dob
    + "\n\t Remark: " + this.remark;

4. Creating JSF Managed Bean File File
Right click  org.ray.jsfbean.controller package--ànew-àJSF Managed BeanàGive class name EmployeeBean-à click finish.
Download attached source code compressed winrar file then copy and paste it.
5. Creating Spring Service  File  and EmployeeServiceImpl File
Right click  org.ray.spring.service package--ànew-àJava ClassàGive class name EmployeService  click finish.
Similarly EmployeeServiceImpl File can be created.
Download attached source code compressed winrar file then copy and paste it.
6.Employee Repository  Files
Download attached source code compressed winrar file then copy and paste it.
7.EmployeeNotFoundException File Download attached source code winrar file then copy and paste it.
Download attached source code compressed winrar file then copy and paste it.
8. crud.xhtml code
Download attached source code compressed winrar file then copy and paste it.
9. faces-config.xml
It is created using notepad and below provided code is added to .save as faces-config.xml.Then copy it and paste to WEB-INF Folder.
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.2"
            <!-- JSF and Spring are integrated -->
10. web.xml (Automatically generated and modified later)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns=""
    <display-name>Employee Management</display-name>

            <!-- Declare Spring configuration file location -->
         <!-- Spring -->
            <!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext.
                        It is registered to Servlet Container -->

            <!-- Project Stage Level -->

            <!-- JSF Servlet is defined to container -->
        <!-- JSF mapping -->
                        <servlet-name>Faces Servlet</servlet-name>

            <!-- Mapping with servlet and url for the http requests. -->
             <!-- Map these files with JSF -->   
        <servlet-name>Faces Servlet</servlet-name>
            <!-- Welcome Page -->
11. spring-database.xml (Add if not aomatically generated and  modified later)
Download attached source code compressed winrar file then copy and paste it.

 crud.xhtml page

Adding New Employee

Edit Employee

Delete Employee

Filtering First Name Starting with ‘S’


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


        <!--Javax inject -->
        <!--Java Annotation Indexer -->

        <!-- aopalliance -->
       <!--Spring Framework-->
        <!--spring-data-commons -->

        <!-- Spring Data JPA dependencies -->
                <!-- aspectjweaver -->
                <!-- querydsl-apt -->

        <!-- aspectjrt -->
        <!--Web Dependencies-->
        <!--Java Server Faces-->
                <!-- hibernate-entitymanager -->

                <!--hibernate-core -->
       <!-- Database dependencies -->
        <!-- MySql Connector -->                     
            <groupId> mysql </groupId>                           
            <artifactId> mysql-connector-java </artifactId>                            
            <version> 5.1.35 </version>                     
        <!-- commons-logging -->

            <name>Repository for library PrimeFaces-maven-lib</name>


Project Files can be downloaded from below URL


  1. Hi,
    Please give link of this Tutorial at below URL of stackoverflow answer so that people can easily find this tutorial

  2. We made easy and Cheap Web Hosting in Pakistan. Many companies are providing webhosting in very high rates with poor quality but PK Domain is still providing cheapest rates of Web Hosting in Pakistan. We believe our Packages are cheapest and best from other companies. In any industry, the best ways to beat your competitors are to offer better products or better services. For more detailed information you can visit us at You can also buy Reseller web hosting, VPS, Dedicated servers, SSL certificates, Security plug-ins, shared hosting and domain names etc.