Spring Data Neo4j

This project provides Spring Data support for the Neo4j Graph Database, including annotated POJOs, SD-Repositories and Neo4j-Template.

Quick Start
Fork me on GitHub

Spring Data Neo4j offers advanced features to map annotated entity classes to the Neo4j Graph Database. The template programming model is equivalent to other Spring templates and builds the basis for interaction with the graph and is also used for the Spring Data repository support. Spring Data Neo4j is core part of the Spring Data project which aims to provide convenient data access for NoSQL databases.

Features

  • Support for property graphs (nodes connected via relationships, each with arbitrary properties)
  • Object-Graph-Mapping of annotated POJO entities
  • extensive Spring Data Commons Repositories Support, including annotated and derived finder methods
  • Arbitrary result projections for finder methods
  • Supports the Cypher Graph Quey Language
  • Support for Neo4j causal clusters
  • Based on the Neo4j-OGM library
  • Supports binary (bolt), http and embedded transports to connect to Neo4j
  • Available with Spring Boot

Quick Start

Download

The recommended way to get started using spring-data-neo4j in your project is with a dependency management system – the snippet below can be copied and pasted into your build. Need help? See our getting started guides on building with Maven and Gradle.

Domain Entities

Annotate your domain objects.

@NodeEntity
public class Movie {

  @Id @GeneratedValue Long id;
  String title;

  Person director;

  @Relationship(type="ACTS_IN", direction = Relationship.INCOMING)
  Set<Person> actors;

  @Relationship(type = "RATED")
  List<Rating> ratings;
}

Repositories

Declare a repository interface.

interface MovieRepository extends Neo4jRepository<Movie, Long> {

  // derived finder
  Movie findByTitle(String title);

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

  // Co-Actors
  Set<Person> findByActorsMoviesActorName(String name);

  @Query("MATCH (movie:Movie)-[:HAS_GENRE]->(genre)<-[:HAS_GENRE]-(similar)
          WHERE id(movie) = {0} RETURN similar")
  List<Movie> findSimilarMovies(Movie movie);
}

Configuration

Enable Spring Data Neo4j repositories.

@Configuration
@EnableTransactionManagement
@ComponentScan("org.neo4j.cineasts")
@EnableNeo4jRepositories("org.neo4j.cineasts.repository")
public class PersistenceContext {

  @Bean
  public SessionFactory getSessionFactory() {
    return new SessionFactory(configuration(), "org.neo4j.cineasts.domain");
  }

  @Bean
  public Neo4jTransactionManager transactionManager() throws Exception {
    return new Neo4jTransactionManager(getSessionFactory());
  }

  @Bean
  public org.neo4j.ogm.config.Configuration configuration() {
    return new org.neo4j.ogm.config.Configuration.Builder()
      .uri("bolt://localhost")
      .build();
  }
}

Example

Use the repository

@Autowired MovieRepository repo;

Iterable<Movie> movies = repo.findAll();
Movie movie = repo.findByTitle("The Matrix");
repo.save(movie);

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