Goals For Java developers who use the Spring Framework or Spring Boot, this guide introduces Spring integration but emphasizes the Spring Data Neo4j library. The library provides convenient access to Neo4j including object mapping, Spring Data repositories, conversion, transaction handling,… Learn More →

Goals
For Java developers who use the Spring Framework or Spring Boot, this guide introduces Spring integration but emphasizes the Spring Data Neo4j library. The library provides convenient access to Neo4j including object mapping, Spring Data repositories, conversion, transaction handling, etc.
Prerequisites
You should be familiar with graph database concepts and the property graph model. Having installed Neo4j and tried out the Cypher query language helps too. You should also be familiar with Spring. Knowing Spring Data and Spring Boot are both great additions to your toolbox as well. When developing with Neo4j, please use JDK 8 and your favorite IDE.
Intermediate

Neo4j for Spring Users

sdn

Neo4j offers a rich set of possibilities for developers using Spring. If you are looking for using Neo4j on the JVM in general, check out our Java Developer’s Guide.

If you want to benefit from full fledged Object Mapping and the other helpful support that comes with Spring Data, use Spring Data Neo4j.

Neo4j’s Spring Data integration was the founding project of the Spring Data efforts, started by Rod Johnson and Emil Eifrem. Spring Data Neo4j was rewritten in 2015 to incorporate the many improvements in Neo4j and Cypher but also in the Spring Data ecosystem. It integrates tightly with the Spring Framework and offers Object-Graph mapping on top of Neo4j.

It is based on the Neo4j-OGM a plain java Object-Graph-Mapper and integrates in the Spring Data infrastructure, including Spring-Data repository, and annotated object mapping support.

Spring Data Neo4j is also supported by Spring Boot. You can use the Spring Boot Starter page to get started with Spring Data Neo4j immediately.

Spring Data Neo4j

Spring Data Neo4j has been around for a long time. Meanwhile Neo4j, it’s APIs and usage developed quickly from an embedded Java only database to a Server solution which is mostly interacted with via Cypher. That’s why we made the decision that a clean slate and reimplementation of the library from scratch is the best approach to embrace the modern Neo4j architecture. In a joint effort with our partner GraphAware the all-new Spring Data Neo4j library was developed and tested by existing users.

Spring Data Neo4j integrates the Neo4j-OGM library to provide fast and comprehensive object graph mapping. Additionally it provides support for Spring Conversions, Transaction Handling, Spring-Data Repositories, Spring-Data REST and Spring-Boot.

To get started, add Spring Data Neo4j as a dependency, then configure the necessary beans in your Java config. Then you can annotate (optional) your entities and define Spring-Data-Repositories as convenient interfaces to your persistence layer.

Features

  • Spring Boot Integration (since 1.4)
  • annotation based object graph mapping
  • interface based repository support with annotated and derived finder methods
  • fast class metadata scanning
  • optimized management of data loading and change tracking for minimal data transfers
  • multiple transports: binary protocol, HTTP and embedded
  • persistence lifecycle events

Quickstart

Spring-Data-Neo4j Dependency
<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>5.0.0.RELEASE</version>
    </dependency>
</dependencies>
Spring-Config
@Configuration
@EnableNeo4jRepositories("org.neo4j.cineasts.repository")
@EnableTransactionManagement
@ComponentScan("org.neo4j.cineasts")
public class PersistenceContext {

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("org.neo4j.cineasts.domain");
    }
}
Entities
@NodeEntity
public class Movie {

  @Id @GeneratedValue Long id;

  String title;

  Person director;

  @Relationship(type="ACTED_IN", direction = Relationship.INCOMING)
  Set<Person> actors = new HashSet<>();
}
Declare a repository interface
interface MovieRepository extends Neo4jRepository<Movie, Long> {

  @Query("MATCH (m:Movie)<-[rating:RATED]-(user)
          WHERE id(m) = {movieId} RETURN rating")
  List<Rating> getRatings(@Param("movieID") Long movieId);

  List<Movie> findByTitle(String title);
}
Use repository
@Autowired MovieRepository repo;

List<Movie> movie = repo.findByTitle("The Matrix");

List<Rating> ratings = repo.getRatings(movieId);

We provide a number of example projects using Spring Data Neo4j, Spring Boot and Web-Frameworks like Angular.js.