PrimeFaces + Spring Data + Neo4j Integration

This tutorial provides example code for integrating PrimeFaces 5, Spring Data and Neo4j technologies.  In our previous tutorials, I have explained about the PrimeFaces and Spring Data integration with other databases MongoDB and Redis. Also read our previous tutorials on installation of Neo4j and Spring Data Neo4j tutorial.

I have used PrimeFaces 5 for this example. At the end of this article, download the source code for the demo. If you have any questions, please write it in the comments section.

Also read:

1. Java Beans

We have used Address and Employee entities in this example.

Address.java

package net.javabeat.springdata.data;

import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;

@NodeEntity
public class Address{

	@GraphId
	private Long id;

	private String addressCountry;

	private String addressCity;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	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;
	}
}

Employee.java

package net.javabeat.springdata.data;

import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;

@NodeEntity
public class Employee{
	@GraphId
	private Long id;

	private String employeeName;

	@Fetch
	@RelatedTo(type="Address")
	private Address address = new Address();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmployeeName() {
		return employeeName;
	}

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

	public Address getAddress() {
		return address;
	}

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

2. Spring Configurations

Spring configuration will contains about the required beans, repositories and the Neo4j service.

SpringContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="

http://www.springframework.org/schema/beans

       	http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<context:component-scan base-package="net.javabeat.springdata.beans"></context:component-scan>

	<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
		<constructor-arg value="http://localhost:7474/db/data/" />
	</bean>

	<neo4j:config graphDatabaseService="graphDatabaseService" base-package="net.javabeat.springdata.data"/>

	<neo4j:repositories base-package="net.javabeat.springdata.repo" />
</beans>

3.  Spring Data Neo4j Repository

For accessing the Neo4j, Spring Data provides the Spring Developer an API for doing that. Spring Data Neo4j allow the developer for accessing the Neo4j to achieve CRUD operations through a pre defined repositories. We have to just implement the repository, rest of the things will be handled by the spring container at run time.

EmployeeRepository.java

package net.javabeat.springdata.repo;

import net.javabeat.springdata.data.Employee;

import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeRepository extends GraphRepository<Employee>{}

4. Spring Bean Service

It’s RegistrationService that already scanned by the Spring Container when the Spring context has initialized. It’s responsible for hosting instances for those defined in repositories.

RegistrationService.java

package net.javabeat.springdata.beans;

import net.javabeat.springdata.repo.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RegistrationService {
	@Autowired
	private EmployeeRepository repo;

	public EmployeeRepository getRepo() {
		return repo;
	}

	public void setRepo(EmployeeRepository repo) {
		this.repo = repo;
	}
}

5. PrimeFaces / JSF Configurations

A non trivial configuration is required when it comes to inject a spring bean into faces managed bean, that’s done by adding a spring expression language resolver into faces-config.xml.

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
	version="2.2">
<application>
	<resource-bundle>
		<base-name>net.javabeat.jsf.application</base-name>
		<var>msg</var>
	</resource-bundle>
	<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>

6. PrimeFaces Managed Bean

RegsitrationManagedBean.java

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 com.google.common.collect.Lists;

import net.javabeat.springdata.beans.RegistrationService;
import net.javabeat.springdata.data.Employee;

@ManagedBean
@SessionScoped
public class RegistrationManagedBean {

	private Employee employee = new Employee();

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

	@ManagedProperty(value="#{registrationService}")
	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.getRepo().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.service.getRepo().save(this.employee);
		this.employee = new Employee();
		return "";
	}
}

7. Web Deployment Descriptor

It’s the deployment descriptor that contains the definition of the web application that is used by the Java EE container.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" metadata-complete="true" version="2.5">
  <context-param>
    <description>State saving method: 'client' or 'server'
						(=default). See JSF Specification 2.5.2
		</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.application.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config/*.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

8. Maven Dependencies

To defining the required dependencies for our example, we’ve used a Maven as a build and dependencies management tool. The pom.xml file will contain the required dependencies.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" metadata-complete="true" version="2.5">
  <context-param>
    <description>State saving method: 'client' or 'server'
						(=default). See JSF Specification 2.5.2
		</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.application.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config/*.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

9. The PrimeFaces 5 View

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
	<h:head>
		<script name="jquery/jquery.js" library="primefaces"></script>
	</h:head>
	<f:view>
		<h:form prependId="false">
			<h2>JavaBeat Tutorials</h2>
			<h2>Primefaces + Spring Data + Neo4j</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>
			</h:panelGrid>
			<p:commandButton value="Register" action="#{registrationManagedBean.register}" ajax="false"/>
			<p:separator/>
			<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>
					<p:column headerText="Employee's Country">
						<h:outputText value="#{employee.address.addressCountry}"/>
					</p:column>
					<p:column headerText="Employee's City">
						<h:outputText value="#{employee.address.addressCity}"/>
					</p:column>
				</p:dataTable>
			</h:panelGrid>
		</h:form>
	</f:view>
</html>

9. PrimeFaces 5 + Spring Data + Neo4j + Spring Web Demo

Neo4j + Spring Data + Primefaces Example 1

Neo4j + Spring Data + Primefaces Example 2

10. Persisted Neo4j Records

If you run the above example, you could see the records in the Neo4j database.

Neo4j + Spring Data + Primefaces Example 3

Neo4j + Spring Data + Primefaces Example 4

Download Source Code

Comments

comments

About Amr Mohammed

Speak Your Mind

*