Sunday, December 7, 2014

Perform CRUD Operations Using Spring-Data-Neo4j.


In this post, we are trying to use Spring-Data-Neo4j with web application to persist the data in the graph form. Spring-Data-Neo4j is the huge wrapper on Neo4j, which is famous graph database community. Neo4j implementation is done by Java , but have support for connectivity with multiple programming languages. Neo4j have two types, one is opensource and other is licensed. 
There are so many graph database engines are available, but still the Neo4j is good product in all open source and have good community support. 

Graph-Database engines are designed in two ways as follow:
  1. Some database engines are native engines, means they store the data in the way of graphs internally. These types of engines are fast and efficient like Neo4j, OrientDB etc 
  2. Some database engines are store the graphs and nodes internally in RDBMS, Object Oriented Database and some other general databases. The example of these graph-database is FoundationDB. These are also called graph layer database. 
In the Graph Database, we create the nodes for store the data and these nodes are connected with each others. Graph Database is the part of No-SQL (Not-Only SQL) but provide ACID operations for store data. For more information go to this link

Graph-Database Storage For RDBMS Developers:

  1. In the table storage, the data is store in the form of records. One table have multiple records. In Graph database one node represent to one record.
  2. In the table storage the one table represent to one entity like User, Address etc and one table have multiple records. In graph storage nodes have labels and we identify multiple records represent to one entity through labels.
  3. In table storage we are use SQL for query the data from tables. In graph storage the query language is depends on graph-db engine like neo4j use cypher query language and orientdb use sql. 
  4. In table storage the relations between tables are manage through primary-key foreign-key relationship for (1:1) and (1:n) or (n:1). If the relation is (m:m) we maintain join tables. In graph-db the relation is by-directional and we also maintain as uni-directional. In these relationship we also set the attributes about relationship, that's why these relationship are called first class relationship. In graph-db relations are individual represent as entity and in graph we easily traverse from different nodes relationship without any headache of sql joins.


In the example, we are creating a flow to create, delete, read and maintain the relationship between nodes. Our example Technoloy stack as follow: 
  1. Spring-Data-Neo4j
  2. Thymeleaf
  3. Junit with Hamcrest
  4. JDK 8
  5. Google Guava
  6. Project Lombok
NOTE: For run the example, firstly configure project lombok with eclipse. For more information go to this link

Download the example code from github as below:

After downloading the code, you need to follow some steps:

  1. Before Import project into eclipse, configure project lombok
  2. Change the database path according to your system in "" file.
  3. Configure neo4j-community application for check nodes as a graphic layout. Download from this link. After installation set the path of db, that is used by example application. 
  4. At one time, the one instance is used Neo4j database like our application or neo4j-community application. Otherwise you get an exception related for lock the database.
  5. When run the JUNIT test, it run successfully, but nodes are not store permanently. When the actual application is run and save the node, the node persist permanently successfully.
  6. After launch an sample application, click on following URL: http://localhost:8080/neo4j-spring-example-web/save-person

Please give you suggestion in comments.