Skip to content
Provides support to increase developer productivity in Java when using the neo4j graph database. Uses familiar Spring concepts such as a template classes for core API usage and provides an annotation based programming model using AspectJ


This branch (master) now points to Spring Data Neo4j 4.x the new implementation that is based on Neo4j-OGM.

Spring Data Neo4j 4.x - Quick start

class Person {
    private Long id;
    private String name;

    @Relationship(type = "FRIEND", direction = "UNDIRECTED")
    private Set<Person> friends;

    public Person() {}
    public Person(String name) { = name; }

    private void knows(Person friend) { friends.add(friend); }

public interface PersonRepository extends GraphRepository<Person> {

Person jon = new Person("Jon");
Person emil = new Person("Emil");
Person rod = new Person("Rod");


// Persist entities and relationships to graph database;

for (Person friend : emil.getFriends()) {
    System.out.println("Friend: " + friend);

// Control loading depth
jon = personRepository.findOne(id, 2);
for (Person friend : jon.getFriends()) {
    System.out.println("Jon's friends to depth 2: " + friend);


The primary goal of the Spring Data project is to make it more convenient and consistent to build Spring-based applications that use modern data technologies. Spring Data Neo4j integrates the leading Neo4j Graph Database.

The Spring Data Neo4j project provides a simplified POJO based programming model that reduces that amount of boilerplate code needed to create Neo4j applications.

It supports:

  • automatic mapping annotated domain entities for nodes and relationships

  • interface based repositories with provided, derived, and annotated finder methods

  • transaction control

  • multi-transport (embedded, http, [bolt])

  • exception translation

  • integration into Spring Data REST

  • works well within Spring Boot

Using Spring Data Neo4j

Maven configuration

  • Add the maven repository and dependency:

        <version>4.1.0.BUILD-SNAPSHOT</version> <!-- or .M1 when released -->
        <name>Springframework Maven MILESTONE Repository</name>

Spring configuration

  • Configure Spring Data Neo4j 4.1 in your application using Java-based bean configuration
@EnableNeo4jRepositories(basePackages = "com.example.person.repository",...)
public class MyConfiguration extends Neo4jConfiguration {

    public SessionFactory getSessionFactory() {
        // with domain entity base package(s)
        return new SessionFactory("com.example.person.domain",...);

    // needed for session in view in web-applications
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();


Spring Data Neo4j 4.1 provides support for connecting to Neo4j using different drivers. HTTP and Embedded drivers are available. Spring Data Neo4j will attempt to auto-configure itself using a file called, which it expects to find on the classpath.

The application can be configured programmatically as well, please read the reference guide for more information.

Graph entities

  • Annotate your entity class. In this case it is a 'Person' class that has a relationship to the 'Company' they work at :

package com.example.person.domain;

class Person {
    private Long id;
    private String name;

    @Relationship(type = "WORKS_AT", direction = "OUTGOING")
    private Company employer;

    public Person() {}
    public Person(String name) { = name; }

    private void worksAt(Company employer) { this.employer = employer; }

Transactional services

Create a repository or service to perform typical operations on your entities. The complete functionality is covered in the reference manual

package com.example.person.repository;

public interface PersonRepository extends GraphRepository<Person> {

   // derived finder method
   Person findByName(String name);

   @Query("MATCH (c:Company)<-[:WORKS_AT]-(p:Person) WHERE id(c) = {company} RETURN p")
   List<Person> findEmployees(Company company);

package com.example.person.service;

public class EmployeeService {

    private PersonRepository personRepository;

    public int getNumberOfPeople() {
        return personRepository.count();

    public Person createPerson(String name) {
        return Person(name));

    public List<Person> getAllPeople() {
        return personRepository.findAll();

    public List<Person> getEmployees(Company c) {
        return personRepository.findEmployees(c);

Please see the SDN University sample project for more information.

More example projects for Spring Data Neo4j 4 are available in the Neo4j-Examples repository

Getting Help

This README and the Reference Manual are the best places to start learning about Spring Data Neo4j 4.

The main SpringSource project site contains links to basic project information such as source code, JavaDocs, Issue tracking, etc.

For more detailed questions, use the "forum": If you are new to Spring as well as to Spring Data, look for information about "Spring projects":

You will also find help on StackOverflow

Contributing to Spring Data Neo4j

There are dedicated, mandatory contribution guidelines for all Spring Data projects.

Here are some ways for you to get involved in the community:

  • Get involved with Spring Data Neo4j community on the Neo4j Google Group and by helping on StackOverflow.

  • Create JIRA tickets for bugs and new features and comment and vote on the ones that you are interested in.

  • Github is for social coding: if you want to write code, we encourage contributions through pull requests from a fork of this repository. If you want to contribute code this way, please read the contribution guidelines for details.

Something went wrong with that request. Please try again.