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 http://www.springsource.org/spring-data/neo4j
Java

README.adoc

Note
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

@NodeEntity
class Person {
    private Long id;
    private String name;

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

    public Person() {}
    public Person(String name) { this.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");

emil.knows(jon);
emil.knows(rod);

// Persist entities and relationships to graph database
personRepository.save(emil);

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);
}

About

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:

pom.xml
<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>4.1.0.BUILD-SNAPSHOT</version> <!-- or .M1 when released -->
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>spring-maven-snapshot</id>
        <snapshots><enabled>true</enabled></snapshots>
        <name>Springframework Maven MILESTONE Repository</name>
        <url>http://maven.springframework.org/milestone</url>
    </repository>
</repositories>

Spring configuration

  • Configure Spring Data Neo4j 4.1 in your application using Java-based bean configuration

MyConfiguration.java
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.person.repository",...)
@EnableTransactionManagement
public class MyConfiguration extends Neo4jConfiguration {

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

    // needed for session in view in web-applications
    @Bean
    @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 ogm.properties, which it expects to find on the classpath.

ogm.properties
driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
URI=http://user:password@localhost:7474

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;

@NodeEntity
class Person {
    private Long id;
    private String name;

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

    public Person() {}
    public Person(String name) { this.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;

@Service
@Transactional
public class EmployeeService {

    @Autowired
    private PersonRepository personRepository;

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

    public Person createPerson(String name) {
        return personRepository.save(new 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":http://forum.springsource.org/forumdisplay.php?f=80. If you are new to Spring as well as to Spring Data, look for information about "Spring projects":http://www.springsource.org/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.