Primefaces, Hibernate and SpringRoo Integration

Filed Under: Hibernate

SpringRoo is an open source software tool that uses convention-over-configuration concept to facilitate producing Java-Based enterprise software application in a driven command-line way. We’ve introduced set of tutorials that get you around creating an enterprise application. Primefaces, Spring Framework, Hibernate, EclipseLink and a different kinds of data stores are combined selectively to help you creating an enterprise application.

This tutorial is different, that is you won’t develop any layers you’ve got developed before. Everything would be assigned for SpringRoo and you will learn the required integration and commands that help you get your first application installed and running.

Final Project Structure

SpringRoo - Final Project Structure

Required Tools

  • SpringRoo 1.2.5
  • JDK 1.6+
  • Apache Tomcat 7
  • Maven 2+
  • Eclipse Kepler 4.3
  • MySQL 5.x

Eclipse Spring Roo Integration

To integrate your Eclipse IDE with a SpringRoo software you have to follow below steps carefully:

  • Download SpringRoo software into your local directory and unzip. For clarification purpose, we’ve unzipped it into D:\SpringRoo\spring-roo-1.2.5.RELEASE.
  • Install SpringRoo plugin into your Eclipse IDE. This is can be done by going into Help – Install New Software and type this given http://dist.springsource.com/release/TOOLS/update/e4.3/ into your Work with address input.

SpringRoo - Install New Software - Fetch All Plugins

  • Check both of Core/Spring IDE and Extensions / Spring IDE.

SpringRoo - Install New Software - Install Required SpringRoo Software

  •   Click Next and finish and wait till the installation process finished.

SpringRoo - Installing New Software

  • Make sure you are restarted your Eclipse IDE. In case you’ve installed Spring Roo successfully, then you are about getting welcome screen like below.

Eclipse IDE Integrated With SpringRoo

  • From Project Explorer, create a new Project and select Spring Roo Project.

Create New Spring Roo Project

  • Click next and type Primefaces-SpringRoo-Hibernate-Sample into Project name. Name your Top level package name and make sure your Project type is standard.

Create New Spring Roo Project - Filling in Required Info

  • Click on Configure Roo Installations and Add your downloaded Spring Roo library.

Add SpringRoo Library Configure Spring Roo Spring Roo Configured

  • Make sure you are selecting a WAR packaging way. Click next and Finish. SpringRoo will start building up your Project using the information you provide.

Spring Roo Is Running

  • Once SpringRoo Project created, you must notice that a SpringRoo Shell Command – Plugin Shell – is opened. In case you’ve faced some obstacles in getting it started and ready, just restart your eclipse and activate the SpringRoo shell once again by clicking on Open Roo Shell for projects.

SpringRoo Project Is Created - SpringRoo Command Shell Is Ready

  • Now, you are ready to start building up your project.

Database View

SpringRoo has facilitated a known paradigm for getting your JPA entities ready for use at a glance. Your Database’s Tables can be reversed engineered in order to eliminate the need of getting additional commands executed. So let’s see what’s the business Tables we’re truly want to implement.

Table Design Diagram

In general, we have two Tables; Employee and Phone which are associated in a One-To-Many association type. Phone Table contains a foreign key refers employeeId field in Employee Table. Following below required SQL create scripts.

EmployeeTable.sql


CREATE TABLE `employee` (
  `EMP_ID` int(11) NOT NULL AUTO_INCREMENT,
  `EMP_NAME` varchar(45) DEFAULT NULL,
  `EMP_HIRE_DATE` varchar(30) DEFAULT NULL,
  `EMP_SALARY` decimal(11,4) DEFAULT NULL,
  PRIMARY KEY (`EMP_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;

PhoneTable.sql


CREATE TABLE `phone` (
  `PH_ID` int(11) NOT NULL AUTO_INCREMENT,
  `PH_NO` varchar(30) DEFAULT NULL,
  `EMP_ID` int(11) DEFAULT NULL,
  PRIMARY KEY (`PH_ID`),
  KEY `PH_EMP_ID_idx` (`EMP_ID`),
  CONSTRAINT `PH_EMP_ID` FOREIGN KEY (`EMP_ID`) REFERENCES `employee` (`EMP_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Developing SpringRoo Application

Developing an enterprise application using a SpringRoo software does contain executing of more than one command against SpringRoo shell. Following a clarified steps for creating wanted application.

  • Setup used persistence framework by executing persistence setup --database MYSQL --provider HIBERNATE --hostName localhost --userName root --password root --databaseName journaldev --persistenceUnit hibernate.jpa

Setup Persistence Layer - Hibernate

  • Install MySQL driver, that would help your SpringRoo software to load the required driver when the process of reverse engineering get started. We have a MySQL driver located on D:\mysql-connector-java-5.1.18-bin.jar. Executing of osgi install --url file:\\\D:\mysql-connector-java-5.1.18-bin.jar would be fair enough. In case you’ve missed out executing this instruction you will get JDBC driver not available for ‘com.mysql.jdbc.Driver’ message when it comes to connect your MySQL database for reverse engineering phase.
  • Create JPA entities, SpringRoo provides you an easy way to create your entities. By executing database reverse engineer --schema journaldev  --package ~.jpa.data --disableVersionFields --activeRecord true --disableGeneratedIdentifiers SpringRoo will scan your database and creating the entities by reversing defined Tables there.

Create Entities

  • Create Primefaces Web layer, one of the options that SpringRoo provides is creating the presentation layer using a JSF implementation. Executing of web jsf setup --implementation ORACLE_MOJARRA --library PRIMEFACES --theme BLUESKY will create all required files for your presentation tier including of required forms like: Employee Registration, Phone Registration, Manage Employees and Manage Phones.

Create Web Layer

  • Create all web stuffs, including utilities beans, required managed beans, converters, messages etc. Executing of web jsf all --package ~.web will help you create all of these stuffs.

Create Managed Beans - Converters - Utilities and All web stuffs

  • For avoiding identifier generation exception, open Employee_Roo_Jpa_Entity.aj and Phone_Roo_Jpa_Entity.aj using Eclipse CTRL+SHIFT+R like below:

Editing Entities - Using Eclipse

  • Add @GeneratedValue(strategy=GenerationType.Auto) annotation onto both of Employee’s identifier and Phone’s identifier.

Employee_Roo_Jpa_Entity.aj


// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
// You may push code into the target .java compilation unit if you wish to edit any member(s).

package com.journaldev.jpa.data;

import com.journaldev.jpa.data.Employee;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

privileged aspect Employee_Roo_Jpa_Entity {
    
    declare @type: Employee: @Entity;
    
    declare @type: Employee: @Table(name = "employee");
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "EMP_ID")
    private Integer Employee.empId;
    
    public Integer Employee.getEmpId() {
        return this.empId;
    }
    
    public void Employee.setEmpId(Integer id) {
        this.empId = id;
    }
}

Phone_Roo_Jpa_Entity.aj


// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
// You may push code into the target .java compilation unit if you wish to edit any member(s).

package com.journaldev.jpa.data;

import com.journaldev.jpa.data.Phone;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

privileged aspect Phone_Roo_Jpa_Entity {
    
    declare @type: Phone: @Entity;
    
    declare @type: Phone: @Table(name = "phone");
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "PH_ID")
    private Integer Phone.phId;
    
    public Integer Phone.getPhId() {
        return this.phId;
    }
    
    public void Phone.setPhId(Integer id) {
        this.phId = id;
    }   
}
  • Execute mvn clean package in order to generate some Java files required by SpringRoo AspectJ does generates and to generate the required WAR for next coming deployment task.
  • Copy your generated WAR file into Apache Tomcat home/webapp folder.

Deploy SpringRoo Application Into Your Apache Tomcat

  • Start up your Apache Tomcat and type the given URL that’s shown below.

Home Page

Demo

Now, you have started your Apache Tomcat and you are staying on the home page for the SpringRoo application. As you’ve noticed, four forms are developed automatically; Create Employee, Create Phone, List All Employees and List All Phones. Let’s take a look before getting started creating new employees and phones.

Database Records - Before Any Operations

Insert New Employee

List of All Employees

Insert New Phone

List of All Phones

Persisted Records

Now, let’s see the records that are persisted into your MySQL database.

Final Records

Summary

SpringRoo has used for Rapid Application Development (RAD), it’s used conventionally for developing an enterprise application in which a Hibernate is the persistence layer and the Primefaces is the presentation layer. Contribute us by commenting below and find downloaded source code.

Comments

  1. Arfaoui chaima says:

    hello Mister
    I tried to import this project into eclipse but I can ‘t and it informed me that this project was not the form of a java project
    the is the error :
    No projects are found to import

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages