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,… Read 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. We cover both the all-new Spring Data Neo4j 4 as well as the previous Spring Data Neo4j 3.
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 familar with Spring. Knowing Spring Data and Spring Boot are both great additions to your toolbox as well. When developing with Neo4j, please use JDK 7 or 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.

In general, using Neo4j’s Java API is always possible from Spring. Just configure an embedded, in-process GraphDatabase as a singleton bean to be injected. For embedded use, Spring Data Neo4j 3 provides great integration too. The integrated variant is also great for unit testing and high-performance/no-network setups.

If you are running Neo4j as a standalone server or cluster, you can access its HTTP-APIs either with a plain RestTemplate or by combining our JDBC-Driver with Spring’s JDBCTemplate. If you want to benefit from full fledged Object Mapping and the other helpful support that comes with Spring Data, check out Spring Data Neo4j 4.

For all Neo4j drivers and integrations, we provide a small, consistent example project across all languages.

The Example Project

The Neo4j example project is a small, one page webapp for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.

You can learn more about our small, consistent example project across many different language drivers here. You will find the implementations for all drivers as individual GitHub repositories, which you can clone and deploy directly.

Spring Integrations

Spring Boot with JDBC
Spring Data Neo4j 4
Spring Data Neo4j 3

Using Spring Boot with JDBC

Spring Boot is a rapid application development infrastructure that allows you to quickly write Spring-based applications with little setup and configuration.

Here we use a simple Spring Boot WebMVC and JDBC application to provide a backend for our movie application.

It uses the well known APIs of @RestController and JdbcTemplate to do the job.

More details can be found in the example project.

Using Spring Data Neo4j 4

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 4 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 4 as a dependency, then configure the necessary beans in your Java config. Then you can use Neo4jTemplate to manage your entities (optionally annotated) and define Spring-Data-Repositories as convenient interfaces to your persistence layer.

Quickstart

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

    @Autowired Environment env;

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

    @Bean
    public Neo4jServer neo4jServer() {
        return new RemoteServer(env.getProperty("neo4j.url"));
    }

    @Override
    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }
}
Entities
@NodeEntity
public class Movie {

  @GraphId Long id;

  String title;

  Person director;

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

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

  Collection<Movie> findByTitle(name);
}
Use repository
@Autowired MovieRepository repo;

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

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

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

Using Spring Data Neo4j 3.3.x

Spring Data Neo4j was the first Spring Data project and enables convenient integration of Neo4j in your Spring-based application. It provides object-graph mapping functionality and other features common to the Spring Data projects.

To use Neo4j with Spring Data Neo4j, you just add the dependency to your build setup. After setting up a simple Java-based Spring configuration, and annotating your @Node- and @RelationshipEntity, you can use Neo4jTemplate to access Neo4j APIs and object-graph mapping functionality. Additionally, you can leverage the convenient Spring Data repositories to get interface-based DAO implementations injected into your Spring Components.

Note
The current version of Spring Data Neo4j works best with an embedded Neo4j database. To achieve highest-performance Neo4j-Server integration, use Spring Data Neo4j 4 or move your SDN persistence layer into an Server Extension.

Quick-Start

<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>3.3.1.RELEASE</version>
    </dependency>
</dependencies>
Entities
@NodeEntity
public class Movie {

  @GraphId Long id;

  @Indexed(type = FULLTEXT, indexName = "search")
  String title;

  Person director;

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

  @RelatedToVia(type = "RATED")
  Iterable<Rating> ratings;
}
Declare a repository interface
interface MovieRepository extends GraphRepository<Movie> {

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

  // Co-Actors
  Iterable<Person> findByActorsMoviesActorName(name)
}
Use repository
@Autowired MovieRepository repo;

Collection<Movie> movie = repo.findByTitleLike("Matrix");

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

The example uses Spring-Boot with Spring-Data-Neo4j and Spring-Data-REST and an additional RestMVCController.

See the example project for further details.