Spring Data + Neo4j II

In this post we will generate a graph data model and store it in Neo4j db using Spring Data repository.

spring-data-neo4j-graph

Objective

  • How to use Spring Data Neo4j Repository?

Environment:

  • Eclipse
  • Embedded Neo4j

Libraries:

  • Spring Framework
  • Spring Data Neo4j
  • Neo4j
  • slf4j
  • AspectJ
  • cglib
  • javax.validator
  • aopalliance
  • asm
  • other refer to .classpath file
 <dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-neo4j</artifactId>
 <version>2.2.0.RELEASE</version>
</dependency>

The Story:

We will build graph data model that represent a set of 100 persons and the relations they have to the people from the same set. The code below generates 100 persons and randomly creates for them relationships to exactly 10 persons from the same set.

( 1 ) Java Project

sping-data-neo4j-ii-java-project

( 2 ) Java Classes

  • Person.java : plain java object with spring data annotations
package com.hmkcode.spring.data.neo4j.entity;

import java.util.HashSet;
import java.util.Set;

import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.Indexed;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;

@NodeEntity
public class Person {

	@Indexed
	private String personId;
	private String personName;

	@GraphId
	private Long nodeId;

	 @RelatedTo(type = "FRIEND_OF", direction = Direction.OUTGOING)
	    private Set<Person> friends;

	public Person(String personId, String personName) {
		this.personId = personId;
	    this.personName = personName;
	}
	public Person(){}
        //setters and getters

	public void addFriend(Person friend) {
		if(this.friends == null)
			this.friends = new HashSet<Person>();
		this.friends.add(friend);
	}

}
  • PersonRepository.java: the magical interface that will give you the default functions to interact with the db without writing implementation for these functions!
package com.hmkcode.spring.data.neo4j.repository;

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

import com.hmkcode.spring.data.neo4j.entity.Person;;

public interface PersonRepository extends GraphRepository<Person> {}
  • PersonService.java: nothing special, just to make your life easier.
  • FriendMaker.java: randomly generates friends when building the data.
  • Main.java: run our app.

Code is available @ github.com/hmkcode

( 3 ) Spring Configuration (spring_aspect.xml)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:spring-configured/>
    <context:annotation-config/>
    <context:component-scan base-package="com.hmkcode.spring.data.neo4j" />

    <neo4j:config storeDirectory="target/neo4j-db"/>
    <neo4j:repositories base-package="com.hmkcode.spring.data.neo4j.repository"/>

    <tx:annotation-driven mode="aspectj"/>
</beans>

( 4 ) AspectJ configuration

You may need to convert the project to AspectJ project from eclipse

  • “right click >> configuration >> convert to AspectJ” you need AspectJ “adjt” eclipse plugin
  • Also, you need to add spring.aspects to aspect-path

( 5 ) Run & Open the webadmin console

after successfully running the code run go to webadmin to check! you should get this.

spring-data-neo4j-graph

Source Code: @ github.com/hmkcode