You are currently viewing A Beginner’s Guide to Spring Data Neo4j

A Beginner’s Guide to Spring Data Neo4j

  • Post author:
  • Post category:Neo4j
  • Post comments:0 Comments
  • Post last modified:May 15, 2024

Introduction to Spring Data Neo4j

Graph databases offer a powerful way to model and query complex relationships between data points. Spring Data Neo4j provides seamless integration between Spring applications and the Neo4j graph database, allowing developers to build robust and scalable applications with ease.

In this tutorial, we’ll walk through the basics of Spring Data Neo4j and demonstrate how to set it up in a Java application. By the end, you’ll have a solid understanding of how to leverage graph databases in your Spring projects.


Before we begin, make sure you have the following installed:

  1. Java Development Kit (JDK)
  2. Apache Maven or Gradle
  3. Neo4j Graph Database

Setting Up the Project

First, let’s create a new Maven project or add the necessary dependencies to an existing one.



Next, we need to configure Spring Data Neo4j to connect to our Neo4j database. Create a configuration class annotated with @Configuration:

import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class Neo4jConfig {

    public SessionFactory sessionFactory() {
        return new SessionFactory(configuration(), "com.example.entity");

    public Neo4jTransactionManager transactionManager() {
        return new Neo4jTransactionManager(sessionFactory());

Replace "com.example" with your package structure.

Creating Entities

Now, let’s define our domain entities that will be mapped to nodes in the graph. Annotate them with @NodeEntity:

import org.neo4j.ogm.annotation.NodeEntity;

public class Person {

    private Long id;
    private String name;

    // Getters and setters

Repository Definition

Create a repository interface that extends Neo4jRepository:


public interface PersonRepository extends Neo4jRepository<Person, Long> {


Service Layer

Optionally, you can create a service layer to encapsulate your business logic:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

public class PersonService {

    private PersonRepository personRepository;

    public Person savePerson(Person person) {

Using the Repository

You can now use your repository in your application:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

public class PersonController {

    private PersonService personService;

    public Person createPerson(@RequestBody Person person) {
        return personService.savePerson(person);


Congratulations! You’ve learned how to integrate Spring Data Neo4j into your Java application. This powerful combination allows you to harness the capabilities of graph databases to build sophisticated and efficient systems. Experiment with different types of queries and explore the full potential of graph-based data modeling. Happy coding!

Leave a Reply