Spring Neo4J

Neo4j is a highly scalable, native graph database. Spring Data supports Spring Neo4J for Graph Database: annotated POJOs, SD-Repositories and Neo4j-Template.

Tutorial will guides you how to work with Spring Neo4j.

I. Technologies for Spring Neo4j tutorial

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE
– Neo4j 3.0.6
– Docker

II. Overview
1. Structure of Project

spring neo4j structure of project

2. Step to do

– Create Spring Boot project
– Add needed dependencies
– Create a Simple Model using Spring Annotation
– Create a GraphRepository
– Create a Controller for interacting with Neo4j database
– Enable Neo4j-Repositories
– Add Neo4j Database config for conneting
– Start Neo4j with Docker
– Build & Run SpringBoot project & Check result

III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info. Then press Finish, spring boot project will be created.

2. Add needed dependencies

Open pom.xml file, add dependencies for data-neo4j and spring-boot-starter-web

Details:

3. Create a Simple Model using Spring Annotation

Create a simple Student model, using Spring Neo4j annotation supported.
@NodeEntity: Identifies a domain entity as being backed by a node in the graph.
@GraphId: Identifies the field in the domain entity which is to be mapped to the id property of its backing node in the graph.

@Relationship: purpose for creating relationship of entities in Neo4j.

Source code:

4. Create a GraphRepository

Create a StudentRepository interface that extends GraphRepository with domain is Student class.

GraphRepository also extends PagingAndSortingRepository

5. Create a Controller for interacting with Neo4j database

Create a simple WebController with 3 Request Mapping:
@RequestMapping(“/addstudents”): for save student entities to Neo4j Database
@RequestMapping(“/addclassmatesforpeter”): add relationship for a Entity which has first-name is “Peter”
@RequestMapping(“/addclassmatesforjack”): add relationship for a Entity with has first-name is “Jack”

6. Enable Neo4j-Repositories

In the main class: SpringNeo4jApplication, add @EnableNeo4jRepositories annotation.

7. Add Neo4j Database config for conneting

In application.properties file, add username/password:

8. Start Neo4j with Docker

Use docker for starting Neo4j

docker spring neo4j

After Neo4j server starts, go to http://localhost:7474/browser/ and change password to admin

9. Build & Run SpringBoot project & Check result

Build project with Maven goal: clean install
Run project with Spring Boot App

Make Requests:
Request 1: http://localhost:8080/addstudents, then check Neo4j database
Now Neo4j has 3 Students.
spring neo4j add data

Request 2: http://localhost:8080/addclassmatesforpeter, then check Neo4j database: Peters has 2 relationships with: Jack and Bob

spring neo4j addclassmateforpeter

Request 3: http://localhost:8080/addclassmatesforjack, then check Neo4j database: Jack has 2 relationships with Peter & Bob

spring neo4j addclassmateforjack

Logs:
spring neo4j logs

IV. Source code

Spring Neo4j

By JavaSampleApproach | October 10, 2016.

Last updated on June 4, 2017.


Related Posts


Got Something To Say:

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

*