Spring Data + Neo4j

This is a simple introduction to Spring Data Neo4j which provides simpler & Spring style development model.

springdata

Objective:

  • How to convert POJO to Neo4j entities?
  • How to interact with Neo4j db using (Neo4jTemplate)?

Environment:

  • Eclipse

Libraries:

  • Spring Framework
  • Spring Data Neo4j
  • spring Data Common
  • Neo4j
  • AspectJ + Validator + slf4j + … alot more

The Story:

We will build this data model using Spring Data Noe4j!

springdataneo4jmodel

( 1 ) Java Project

( 2 ) Java Classes

Person.java

package com.hmkcode.vo;

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 {

	@GraphId
	private Long nodeId;

	@Indexed
	private String id;
	private String name;

	public Person(){}

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

	//getters and setters....

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

GraphDB.java

package com.hmkcode.springdata.neo4j;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.support.Neo4jTemplate;

import com.hmkcode.vo.*;

public class GraphDB {

	 @Autowired         
	 private Neo4jTemplate template; 

	 public void buildDataModel(){
		registerShutdownHook( template.getGraphDatabaseService());
		Transaction tx = template.getGraphDatabase().beginTx();
		try{
			Person p1 = new Person();
			p1.setId("person-01");
			p1.setName("John");
			Person p2 = new Person();
			p2.setId("person-02");
			p2.setName("Brit");
			Person p3 = new Person();
			p3.setId("person-03");
			p3.setName("Adam");
			p2.addFriend(p3);
			p1.addFriend(p2);

			Person savedPerson = template.save(p1);

			Person loadedPerson = template.findOne(savedPerson.getNodeId(), Person.class);

			System.out.println("User: "+loadedPerson.getNodeId());
			tx.success();
			tx.finish();
		}finally{
			 System.out.println("finally");
		 }
	 }

	public Neo4jTemplate getTemplate() {
		return template;
	}

	public void setTemplate(Neo4jTemplate template) {
		this.template = template;
	}
	private static void registerShutdownHook( final GraphDatabaseService graphDb )
	{
	    Runtime.getRuntime().addShutdownHook( new Thread()
	    {
	        @Override
	        public void run()
	        {
	            graphDb.shutdown();

	        }
	    } );
	}	
}

Main.java

package com.hmkcode;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hmkcode.springdata.neo4j.GraphDB;

public class Main{

	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("com/hmkcode/spring/spring.xml");
		GraphDB db = (GraphDB)context.getBean("graphDB");
		db.buildDataModel();
	}	
}

( 3 ) Spring Configuration

<?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:tx="http://www.springframework.org/schema/tx"         
		xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"         
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
		xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


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


http://www.springframework.org/schema/tx/spring-tx-3.0.xsd


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


http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/data/neo4j


http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd">

		<context:annotation-config />          

		<neo4j:config storeDirectory="graphdb/graph.db"  />  

		<bean id="graphDB" class="com.hmkcode.springdata.neo4j.GraphDB">                 
			<property name="template" ref="neo4jTemplate">                 
			</property>         
		</bean>  

</beans>

( 4 ) Run the Code

After running the code 3 nodes will be inserted. Use webadmin to verify!

Source Code: Github