© 2008-2020 The original authors.

Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.

Preface

1. Your way through this document

If you already familiar with the core concepts of Spring Data, head straight to Chapter 4. This chapter will walk you through different options of configuring an application to connect to a Neo4j instance and how to model your domain.

In most cases, you will need a domain. Go to Chapter 5 to learn about how to map nodes and relationships to your domain model.

After that, you will need some means to query the domain. Choices are Neo4j repositories, the Neo4j Template or on a lower level, the Neo4j Client. All of them are available in a reactive fashion as well. Apart from the paging mechanism, all the features of standard repositories are available in the reactive variant.

You will find the building blocks in the next chapter.

To learn more about the general concepts of repositories, head over to [repositories].

You can of course read on, continuing with the preface, and a gentle getting started guide.

2. NoSQL and Graph databases

A graph database is a storage engine that is specialized in storing and retrieving vast networks of information. It efficiently stores data as nodes with relationships to other or even the same nodes, thus allowing high-performance retrieval and querying of those structures. Properties can be added to both nodes and relationships. Nodes can be labelled by zero or more labels, relationships are always directed and named.

Graph databases are well suited for storing most kinds of domain models. In almost all domains, there are certain things connected to other things. In most other modeling approaches, the relationships between things are reduced to a single link without identity and attributes. Graph databases allow to keep the rich relationships that originate from the domain equally well-represented in the database without resorting to also modeling the relationships as "things". There is very little "impedance mismatch" when putting real-life domains into a graph database.

2.1. Introducing Neo4j

Neo4j is an open source NoSQL graph database. It is a fully transactional database (ACID) that stores data structured as graphs consisting of nodes, connected by relationships. Inspired by the structure of the real world, it allows for high query performance on complex data, while remaining intuitive and simple for the developer.

The starting point for learning about Neo4j is neo4j.com. Here is a list of useful resources:

2.2. Spring and Spring Data

Spring Data uses Spring Framework’s core functionality, such as the IoC container, type conversion system, expression language, JMX integration, and portable DAO exception hierarchy. While it is not necessary to know all the Spring APIs, understanding the concepts behind them is. At a minimum, the idea behind IoC should be familiar.

The Spring Data Neo4j project applies Spring Data concepts to the development of solutions using the Neo4j graph data store. We provide repositories as a high-level abstraction for storing and querying documents as well as templates and clients for generic domain access or generic query execution. All of them are integrated with Spring’s application transactions.

The core functionality of the Neo4j support can be used directly, through either the Neo4jClient or the Neo4jTemplate or the reactive variants thereof. All of them provide integration with Spring’s application level transactions. On a lower level, you can grab the Bolt driver instance, but than you have to manage your own transactions.

To learn more about Spring, you can refer to the comprehensive documentation that explains in detail the Spring Framework. There are a lot of articles, blog entries and books on the matter - take a look at the Spring Framework home page for more information.

2.3. What is Spring Data Neo4j

The current Spring Data Neo4j is the successor to Spring Data Neo4j + Neo4j-OGM. The separate layer of Neo4j-OGM (Neo4j Object Graph Mapper) has been replaced by Spring infrastructure, but the basic concepts of an Object Graph Mapper (OGM) still apply:

An OGM maps nodes and relationships in the graph to objects and references in a domain model. Object instances are mapped to nodes while object references are mapped using relationships, or serialized to properties (e.g. references to a Date). JVM primitives are mapped to node or relationship properties. An OGM abstracts the database and provides a convenient way to persist your domain model in the graph and query it without having to use low level drivers directly. It also provides the flexibility to the developer to supply custom queries where the queries generated by SDN are insufficient.

2.3.1. What’s in the box?

Spring Data Neo4j or in short SDN is a next-generation Spring Data module, created and maintained by Neo4j, Inc. in close collaboration with Pivotal’s Spring Data Team.

SDN relies completely on the Neo4j Java Driver, without introducing another "driver" or "transport" layer between the mapping framework and the driver. The Neo4j Java Driver - sometimes dubbed Bolt or the Bolt driver - is used as a protocol much like JDBC is with relational databases.

Noteworthy features that differentiate the new SDN from Spring Data Neo4j + OGM are

  • Full support for immutable entities and thus full support for Kotlin’s data classes

  • Full support for the reactive programming model in the Spring Framework itself and Spring Data

  • Brand new Neo4j client and reactive client feature, resurrecting the idea of a template over the plain driver, easing database access

2.3.2. Why should I use SDN in favor of SDN+OGM

SDN has several features not present in SDN+OGM, notably

  • Full support for Springs reactive story, including reactive transaction

  • Full support for Query By Example

  • Full support for fully immutable entities

  • Support for all modifiers and variations of derived finder methods, including spatial queries

2.3.3. How does SDN relate to Neo4j-OGM?

Neo4j-OGM is an Object Graph Mapping library, which is mainly used by Spring Data Neo4j as its backend for the heavy lifting of mapping nodes and relationships into domain object. The new SDN does not need and does not support Neo4j-OGM. SDN uses Spring Data’s mapping context exclusively for scanning classes and building the meta model.

While this pins SDN to the Spring eco systems, it has several advantages, among them the smaller footprint in regards of CPU and memory usage and especially, all the features of Springs mapping context.

2.3.4. Does SDN support connections over HTTP to Neo4j?

No.

2.3.5. Does SDN support embedded Neo4j?

Embedded Neo4j has multiple facets to it:

Does SDN provide an embedded instance for your application?

No.

Does SDN interact directly with an embedded instance?

No. An embedded database is usually represented by an instance of org.neo4j.graphdb.GraphDatabaseService and has no Bolt connector out of the box.

SDN can however work very much with Neo4j’s test harness, the test harness is specially meant to be a drop-in replacement for the real database. Support for both Neo4j 3.5 and 4.0 test harness is implemented via the Spring Boot starter for the driver. Have a look at the corresponding module org.neo4j.driver:neo4j-java-driver-test-harness-spring-boot-autoconfigure.

Can I use SDN without Spring Boot?

Yes, see our README. We provide org.springframework.data.neo4j.config.AbstractNeo4jConfig and org.springframework.data.neo4j.config.AbstractReactiveNeo4jConfig for that purpose.

3. Building blocks

3.1. Overview

SDN consists of composable building blocks. It builds on top of the Neo4j Java Driver. The instance of the Java driver is provided through Spring Boot’s automatic configuration itself. All configuration options of the driver are accessible in the namespace spring.neo4j. The driver bean provides imperative, asynchronous and reactive methods to interact with Neo4j.

You can use all transaction methods the driver provides on that bean such as auto-commit transactions, transaction functions and unmanaged transactions. Be aware that those transactions are not tight to an ongoing Spring transaction.

Integration with Spring Data and Spring’s platform or reactive transaction manager starts at the Neo4j Client. The client is part of SDN is configured through a separate starter, spring-boot-starter-data-neo4j. The configuration namespace of that starter is spring.data.neo4j.

The client is mapping agnostic. It doesn’t know about your domain classes and you are responsible for mapping a result to an object suiting your needs.

The next higher level of abstraction is the Neo4j Template. It is aware of your domain and you can use it to query arbitrary domain objects. The template comes in handy in scenarios with a large number of domain classes or custom queries for which you don’t want to create an additional repository abstraction each.

The highest level of abstraction is a Spring Data repository.

All abstractions of SDN come in both imperative and reactive fashions. It is not recommend to mix both programming styles in the same application. The reactive infrastructure requires a Neo4j 4.0+ database.

sdn buildingblocks
Figure 1. SDN building blocks

The template mechanism is similar to the templates of others stores. Find some more information about it in our FAQ. The Neo4j Client as such is unique to SDN. You will find it’s documentation in the appendix.

3.2. On the package level

Package Description

org.springframework.data.neo4j.config

Unresolved directive in introduction-and-preface/building-blocks.adoc - include::../../../main/java/org/springframework/data/neo4j/config/package-info.java[tags=intent,indent=0]

org.springframework.data.neo4j.core

Unresolved directive in introduction-and-preface/building-blocks.adoc - include::../../../main/java/org/springframework/data/neo4j/core/package-info.java[tags=intent,indent=0]

org.springframework.data.neo4j.core.convert

Unresolved directive in introduction-and-preface/building-blocks.adoc - include::../../../main/java/org/springframework/data/neo4j/core/convert/package-info.java[tags=intent,indent=]

org.springframework.data.neo4j.core.support

Unresolved directive in introduction-and-preface/building-blocks.adoc - include::../../../main/java/org/springframework/data/neo4j/core/support/package-info.java[tags=intent,indent=]

org.springframework.data.neo4j.core.transaction

Unresolved directive in introduction-and-preface/building-blocks.adoc - include::../../../main/java/org/springframework/data/neo4j/core/transaction/package-info.java[tags=intent,indent=]

org.springframework.data.neo4j.repository

Unresolved directive in introduction-and-preface/building-blocks.adoc - include::../../../main/java/org/springframework/data/neo4j/repository/package-info.java[tags=intent,indent=0]

org.springframework.data.neo4j.repository.config

Unresolved directive in introduction-and-preface/building-blocks.adoc - include::../../../main/java/org/springframework/data/neo4j/repository/config/package-info.java[tags=intent,indent=0]

org.springframework.data.neo4j.repository.support

Unresolved directive in introduction-and-preface/building-blocks.adoc - include::../../../main/java/org/springframework/data/neo4j/repository/support/package-info.java[tags=intent,indent=0]

Unresolved directive in index.adoc - include::../../../../../spring-data-commons/src/main/asciidoc/dependencies.adoc[leveloffset=+1] Unresolved directive in index.adoc - include::../../../../../spring-data-commons/src/main/asciidoc/repositories.adoc[leveloffset=+1]

Reference Documentation

Who should read this?

This manual is written for:

  • the enterprise architect investigating Spring integration for Neo4j.

  • the engineer developing Spring Data based applications with Neo4j.

4. Getting started

We provide a Spring Boot starter for SDN. Please include the starter module via your dependency management and configure the bolt URL to use, for example org.neo4j.driver.uri=bolt://localhost:7687. The starter assumes that the server has disabled authentication. As the SDN starter depends on the starter for the Java Driver, all things regarding configuration said there, apply here as well. For a reference of the available properties, use your IDEs autocompletion in the org.neo4j.driver namespace or look at the dedicated manual.

SDN supports

  • The well known and understood imperative programming model (much like Spring Data JDBC or JPA)

  • Reactive programming based on Reactive Streams, including full support for reactive transactions.

Those are all included in the same binary. The reactive programming model requires a 4.0 Neo4j server on the database side and reactive Spring on the other hand. Have a look at the examples directory for all examples.

4.1. Prepare the database

For this example, we stay within the movie graph, as it comes for free with every Neo4j instance.

If you don’t have a running database but Docker installed, please run:

Listing 1. Start a local Neo4j instance inside Docker.
docker run --publish=7474:7474 --publish=7687:7687 -e 'NEO4J_AUTH=neo4j/secret' neo4j:4.0.4

You know can access http://localhost:7474. The above command sets the password of the server to secret. Note the command ready to run in the prompt (:play movies). Execute it to fill your database with some test data.

4.2. Create a new Spring Boot project

The easiest way to setup a Spring Boot project is start.spring.io (which is integrated in the major IDEs as well, in case you don’t want to use the website).

Select the "Spring Web Starter" to get all the dependencies needed for creating a Spring based web application. The Spring Initializr will take care of creating a valid project structure for you, with all the files and settings in place for the selected build tool.

Don’t choose Spring Data Neo4j here, as it will get you the previous generation of Spring Data Neo4j including OGM and additional abstraction over the driver.

You might want to follow this link for a preconfigured setup.

4.2.1. Using Maven

You can issue a curl request against the Spring Initializer to create a basic Maven project:

Listing 2. Create a basic Maven project with the Spring Initializr
curl https://start.spring.io/starter.tgz \
  -d dependencies=webflux,actuator \
  -d bootVersion=2.4.0.M3 \
  -d baseDir=Neo4jSpringBootExample \
  -d name=Neo4j%20SpringBoot%20Example | tar -xzvf -

This will create a new folder Neo4jSpringBootExample. As this starter is not yet on the initializer, you will have to add the following dependency manually to your pom.xml:

Listing 3. Inclusion of the spring-data-neo4j-spring-boot-starter in a Maven project
<dependency>
	<groupId>{groupId}</groupId>
	<artifactId>spring-boot-starter-data-neo4j</artifactId>
	<version>6.0.0-RC2</version>
</dependency>

You would also add the dependency manually in case of an existing project.

4.2.2. Using Gradle

The idea is the same, just generate a Gradle project:

Listing 4. Create a basic Gradle project with the Spring Initializr
curl https://start.spring.io/starter.tgz \
  -d dependencies=webflux,actuator \
  -d type=gradle-project \
  -d bootVersion=2.4.0.M3 \
  -d baseDir=Neo4jSpringBootExampleGradle \
  -d name=Neo4j%20SpringBoot%20Example | tar -xzvf -

The dependency for Gradle looks like this and must be added to build.gradle:

Listing 5. Inclusion of the spring-data-neo4j-spring-boot-starter in a Gradle project
dependencies {
    implementation '{groupId}:spring-boot-starter-data-neo4j:6.0.0-RC2'
}

You would also add the dependency manually in case of an existing project.

4.3. Configure the project

Now open any of those projects in your favorite IDE. Find application.properties and configure your Neo4j credentials:

org.neo4j.driver.uri=bolt://localhost:7687
org.neo4j.driver.authentication.username=neo4j
org.neo4j.driver.authentication.password=secret

This is the bare minimum of what you need to connect to a Neo4j instance.

It is not necessary to add any programmatically configuration of the driver when you use this starter. SDN repositories will be automatically enabled by this starter.

4.4. Create your domain

Our domain layer should accomplish two things:

  • Map your graph to objects

  • Provide access to those

4.4.1. Example Node-Entity

SDN fully supports unmodifiable entities, for both Java and data classes in Kotlin. Therefor we will focus on immutable entities here, Listing 6 shows a such an entity.

SDN supports all data types the Neo4j Java Driver supports, see Map Neo4j types to native language types inside the chapter "The Cypher type system". Future versions will support additional converters.
Listing 6. MovieEntity.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.data.neo4j.core.schema.Relationship;
import org.springframework.data.neo4j.core.schema.Relationship.Direction;

@Node("Movie") (1)
public class MovieEntity {

	@Id (2)
	private final String title;

	@Property("tagline") (3)
	private final String description;

	@Relationship(type = "ACTED_IN", direction = Direction.INCOMING) (4)
	private Map<PersonEntity, Roles> actorsAndRoles = new HashMap<>();

	@Relationship(type = "DIRECTED", direction = Direction.INCOMING) private List<PersonEntity> directors = new ArrayList<>();

	public MovieEntity(String title, String description) { (5)
		this.title = title;
		this.description = description;
	}

	// Getters omitted for brevity
}
1 @Node is used to mark this class as a managed entity. It also is used to configure the Neo4j label. The label defaults to the name of the class, if you’re just using plain @Node.
2 Each entity has to have an id. The movie class shown here uses the attribute title as a unique business key. If you don’t have such a unique key, you can use the combination of @Id and @GeneratedValue to configure SDN to use Neo4j’s internal id. We also provide generators for UUIDs.
3 This shows @Property as a way to use a different name for the field than for the graph property.
4 This defines a relationship to a class of type PersonEntity and the relationship type ACTED_IN
5 This is the constructor to be used by your application code.

As a general remark: Immutable entities using internally generated ids are a bit contradictory, as SDN needs a way to set the field with the value generated by the database.

If you don’t find a good business key or don’t want to use a generator for IDs, here’s the same entity using the internally generated id together with a businesses constructor and a so called wither-Method, that is used by SDN:

Listing 7. MovieEntity.java
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;

import org.springframework.data.annotation.PersistenceConstructor;

@Node("Movie")
public class MovieEntity {

	@Id @GeneratedValue
	private Long id;

	private final String title;

	@Property("tagline")
	private final String description;

	public MovieEntity(String title, String description) { (1)
		this.id = null;
		this.title = title;
		this.description = description;
	}

	public MovieEntity withId(Long id) { (2)
		if (this.id.equals(id)) {
			return this;
		} else {
			MovieEntity newObject = new MovieEntity(this.title, this.description);
			newObject.id = id;
			return newObject;
		}
	}
}
1 This is the constructor to be used by your application code. It sets the id to null, as the field containing the internal id should never be manipulated.
2 This is a so-called wither for the id-attribute. It creates a new entity and sets the field accordingly, without modifying the original entity, thus making it immutable.

You can of course use SDN with Kotlin and model your domain with Kotlin’s data classes. Project Lombok is an alternative if you want or need to stay purely within Java.

4.4.2. Declaring Spring Data repositories

You basically have two options here: You can work store agnostic with SDN and make your domain specific extends one of

  • org.springframework.data.repository.Repository

  • org.springframework.data.repository.CrudRepository

  • org.springframework.data.repository.reactive.ReactiveCrudRepository

  • org.springframework.data.repository.reactive.ReactiveSortingRepository

Choose imperative and reactive accordingly.

While technically not prohibited, it is not recommended to mix imperative and reactive database access in the same application. We won’t support you with scenarios like this.

The other option is to settle on a store specific implementation and gain all the methods we support out of the box. The advantage of this approach is also it’s biggest disadvantage: Once out, all those methods will be part of your API. Most of the time it’s harder to take something away, than to add stuff afterwards. Furthermore, using store specifics leaks your store into your domain. From a performance point of view, there is no penalty.

A repository fitting to any of the movie entities above looks like this:

Listing 8. MovieRepository.java
import reactor.core.publisher.Mono;

import org.springframework.data.neo4j.repository.ReactiveNeo4jRepository;

public interface MovieRepository extends ReactiveNeo4jRepository<MovieEntity, String> {

	Mono<MovieEntity> findOneByTitle(String title);
}
Testing reactive code is done with a reactor.test.StepVerifier. Have a look at the corresponding documentation of Project Reactor or see our example code.

5. Object Mapping

The following sections will explain the process of mapping between your graph and your domain. It is split into two parts. The first part explains the actual mapping and the available tools for you to describe how to map nodes, relationships and properties to objects. The second part will have a look at Spring Data’s object mapping fundamentals. It gives valuable tips on general mapping, why you should prefer immutable domain objects and how you can model them with Java or Kotlin.

5.1. Metadata-based Mapping

To take full advantage of the object mapping functionality inside SDN, you should annotate your mapped objects with the @Node annotation. Although it is not necessary for the mapping framework to have this annotation (your POJOs are mapped correctly, even without any annotations), it lets the classpath scanner find and pre-process your domain objects to extract the necessary metadata. If you do not use this annotation, your application takes a slight performance hit the first time you store a domain object, because the mapping framework needs to build up its internal metadata model so that it knows about the properties of your domain object and how to persist them.

5.1.1. Mapping Annotation Overview

From SDN:
  • @Node: Applied at the class level to indicate this class is a candidate for mapping to the database.

  • @Id: Applied at the field level to mark the field used for identity purpose.

  • @GeneratedValue: Applied at the field level together with @Id to specify how unique identifiers should be generated.

  • @Property: Applied at the field level to modify the mapping from attributes to properties.

  • @CompositeProperty: Applied at the field level on attributes of type Map that shall be read back as a composite. See Composite properties.

  • @Relationship: Applied at the field level to specify the details of a relationship.

  • @DynamicLabels: Applied at the field level to specify the source of dynamic labels.

  • @RelationshipProperties: Applied at the class level to indicate this class as the target for properties of a relationship.

  • @TargetNode: Applied on a field of a class annotated with @RelationshipProperties to mark the target of that relationship from the perspective of the other end.

The following annotations are used to specify conversions and ensure backwards compatibility with OGM.

  • @DateLong

  • @DateString

  • @ConvertWith

See Conversions for more information on that.

From Spring Data commons
  • @org.springframework.data.annotation.Id same as @Id from SDN, in fact, @Id is annotated with Spring Data Common’s Id-annotation.

  • @CreatedBy: Applied at the field level to indicate the creator of a node.

  • @CreatedDate: Applied at the field level to indicate the creation date of a node.

  • @LastModifiedBy: Applied at the field level to indicate the author of the last change to a node.

  • @LastModifiedDate: Applied at the field level to indicate the last modification date of a node.

  • @PersistenceConstructor: Applied at one constructor to mark it as a the preferred constructor when reading entities.

  • @Persistent: Applied at the class level to indicate this class is a candidate for mapping to the database.

  • @Version: Applied at field level is used for optimistic locking and checked for modification on save operations. The initial value is zero which is bumped automatically on every update.

Have a look at [auditing] for all annotations regarding auditing support.

5.1.2. The basic building block: @Node

The @Node annotation is used to mark a class as a managed domain class, subject to the classpath scanning by the mapping context.

To map an Object to nodes in the graph and vice versa, we need a label to identify the class to map to and from.

@Node has an attribute labels that allows you to configure one or more labels to be used when reading and writing instances of the annotated class. The value attribute is an alias for labels. If you don’t specify a label, than the simple class name will be used as the primary label. In case you want to provide multiple labels, you could either:

  1. Supply an array to the labels property. The first element in the array will considered as the primary label.

  2. Supply a value for primaryLabel and put the additional labels in labels.

The primary label should always be the most concrete label that reflects your domain class.

For each instance of an annotated class that is written through a repository or through the Neo4j template, one node in the graph with at least the primary label will be written. Vice versa, all nodes with the primary label will be mapped to the instances of the annotated class.

The @Node annotation is not inherited from super-types and interfaces. You can however annotate your domain classes individually at every inheritance level. This allows polymorphic queries: You can pass in base or intermediate classes and retrieve the correct, concrete instance for your nodes. This is only supported for abstract bases annotated with @Node. The labels defined on such a class will be used as additional labels together with the labels of the concrete implementations.

Dynamic or "runtime" managed labels

All labels implicitly defined through the simple class name or explicitly via the @Node annotation are static. They cannot be changed during runtime. If you need additional labels that can be manipulated during runtime, you can use @DynamicLabels. @DynamicLabels is an annotation on field level and marks an attribute of type java.util.Collection<String> (a List or Set) for example) as source of dynamic labels.

If this annotation is present, all labels present on a node and not statically mapped via @Node and the class names, will be collected into that collection during load. During writes, all labels of the node will be replaced with the statically defined labels plus the contents of the collection.

If you have other applications add additional labels to nodes, don’t use @DynamicLabels. If @DynamicLabels is present on a managed entity, the resulting set of labels will be "the truth" written to the database.

5.1.3. Identifying instances: @Id

While @Node creates a mapping between a class and nodes having a specific label, we also need to make the connection between individual instances of that class (objects) and instances of the node.

This is where @Id comes into play. @Id marks an attribute of the class to be the unique identifier of the object. That unique identifier is in an optimal world a unique business key or in other words, a natural key. @Id can be used on all attributes with a supported simple type.

Natural keys are however pretty hard to find. Peoples names for example are seldom unique, change over time or worse, not everyone has a first and last name.

We therefore support two different kind of surrogate keys.

On an attribute of type long or Long, @Id can be used with @GeneratedValue. This maps the Neo4j internal id, which is not a property on a node or relationship and usually not visible, to the attribute and allows SDN to retrieve individual instances of the class.

@GeneratedValue provides the attribute generatorClass. generatorClass can be used to specify a class implementing IdGenerator. An IdGenerator is a functional interface and its generateId takes the primary label and the instance to generate an Id for. We support UUIDStringGenerator as one implementation out of the box.

You can also specify a Spring Bean from the application context on @GeneratedValue via generatorRef. That bean also needs to implement IdGenerator, but can make use of everything in the context, including the Neo4j client or template to interact with the database.

Don’t skip the important notes about ID handling in Section 5.2

5.1.4. Mapping properties: @Property

All attributes of a @Node-annotated class will be persisted as properties of Neo4j nodes and relationships. Without further configuration, the name of the attribute in the Java or Kotlin class will be used as Neo4j property.

If you are working with an existing Neo4j schema or just like to adapt the mapping to your needs, you will need to use @Property. The name is used to specify the name of the property inside the database.

5.1.5. Connecting nodes: @Relationship

The @Relationship annotation can be used on all attributes that are not a simple type. It is applicable on attributes of other types annotated with @Node or collections and maps thereof.

The type or the value attribute allow configuration of the relationship’s type, direction allows specifying the direction. The default direction in SDN is Relationship.Direction#OUTGOING.

We support dynamic relationships. Dynamic relationships are represented as a Map<String, AnnotatedDomainClass>. In such a case, the type of the relationship to the other domain class is given by the maps key and must not be configured through the @Relationship.

Map relationship properties

Neo4j supports defining properties not only on nodes but also on relationships. To express those properties in the model SDN provides @RelationshipProperties to be applied on a simple Java class.

In the entity class the relationship can be modelled as before but its type has to be a Map with the related node as the key and the relation property class as value.

A relationship property class and its usage may look like this:

Listing 9. Relationship properties Roles
@RelationshipProperties
public class Roles {

	private final List<String> roles;

	public Roles(List<String> roles) {
		this.roles = roles;
	}

	public List<String> getRoles() {
		return roles;
	}
}
Listing 10. Defining relationship properties for an entity
private Map<PersonEntity, Roles> actorsAndRoles = new HashMap<>();
Relationship query limit

In general there is no limitation of relationships / hops for creating the queries. SDN parses the whole reachable graph from your modelled nodes. It is possible to have self-referencing entities and self-referencing concrete instances.

If these entities or instances for a cycle we have a strict limit of 2 repetitions of walking the same path through the graph. E.g. You model a social network of (:Person)-[:HAS_FRIEND]→(:Person)-[:HAS_FRIEND]…​ you will only get the friends of the second degree. If you need a more specific mapping for your domain we advise you to use custom queries.

5.1.6. A complete example

Putting all those together, we can create a simple domain. We use movies and people with different roles:

Example 1. The MovieEntity
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.data.neo4j.core.schema.Relationship;
import org.springframework.data.neo4j.core.schema.Relationship.Direction;

@Node("Movie") (1)
public class MovieEntity {

	@Id (2)
	private final String title;

	@Property("tagline") (3)
	private final String description;

	@Relationship(type = "ACTED_IN", direction = Direction.INCOMING) (4)
	private Map<PersonEntity, Roles> actorsAndRoles = new HashMap<>();

	@Relationship(type = "DIRECTED", direction = Direction.INCOMING) private List<PersonEntity> directors = new ArrayList<>();

	public MovieEntity(String title, String description) { (5)
		this.title = title;
		this.description = description;
	}

	// Getters omitted for brevity
}
1 @Node is used to mark this class as a managed entity. It also is used to configure the Neo4j label. The label defaults to the name of the class, if you’re just using plain @Node.
2 Each entity has to have an id. We use the movie’s name as unique identifier.
3 This shows @Property as a way to use a different name for the field than for the graph property.
4 This configures an incoming relationship to a person.
5 This is the constructor to be used by your application code as well as by SDN.

People are mapped in two roles here, actors and directors. The domain class is the same:

Example 2. The PersonEntity
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;

@Node("Person")
public class PersonEntity {

	@Id private final String name;

	private final Integer born;

	public PersonEntity(Integer born, String name) {
		this.born = born;
		this.name = name;
	}

	public Integer getBorn() {
		return born;
	}

	public String getName() {
		return name;
	}

}
We haven’t modelled the relationship between movies and people in both direction. Why is that? We see the MovieEntity as the aggregate root, owning the relationships. On the other hand, we want to be able to pull all people from the database without selecting all the movies associated with them. Please consider your applications use case before you try to map every relationship in your database in every direction. While you can do this, you may end up rebuilding a graph database inside your object graph and this is not the intention of a mapping framework.

5.2. Handling and provisioning of unique IDs

5.2.1. Using the internal Neo4j id

The easiest way to give your domain classes an unique identifier is the combination of @Id and @GeneratedValue on a field of type Long (preferable the object, not the scalar long, as literal null is the better indicator whether an instance is new or not):

Example 3. Mutable MovieEntity with internal Neo4j id
@Node("Movie")
public class MovieEntity {

	@Id @GeneratedValue
	private Long id;

	private String name;

	public MovieEntity(String name) {
		this.name = name;
	}
}

You don’t need to provide a setter for the field, SDN will use reflection to assign the field, but use a setter if there is one. If you want to create an immutable entity with an internally generated id, you have to provide a wither.

Example 4. Immutable MovieEntity with internal Neo4j id
@Node("Movie")
public class MovieEntity {

	@Id @GeneratedValue
	private final Long id; (1)

	private String name;

	public MovieEntity(String name) { (2)
		this(null, name);
	}

	private MovieEntity(Long id, String name) { (3)
		this.id = id;
		this.name = name;
	}

	public MovieEntity withId(Long id) { (4)
		if (this.id.equals(id)) {
			return this;
		} else {
			return new MovieEntity(id, this.title);
		}
	}
}
1 Immutable final id field indicating a generated value
2 Public constructor, used by the application and Spring Data
3 Internally used constructor
4 This is a so-called wither for the id-attribute. It creates a new entity and set’s the field accordingly, without modifying the original entity, thus making it immutable.

You either have to provide a setter for the id attribute or something like a wither, if you want to have

  • Advantages: It is pretty clear that the id attribute is the surrogate business key, it takes no further effort or configuration to use it.

  • Disadvantage: It is tied to Neo4js internal database id, which is not unique to our application entity only over a database lifetime.

  • Disadvantage: It takes more effort to create an immutable entity

5.2.2. Use externally provided surrogate keys

The @GeneratedValue annotation can take a class implementing org.springframework.data.neo4j.core.schema.IdGenerator as parameter. SDN provides InternalIdGenerator (the default) and UUIDStringGenerator out of the box. The later generates new UUIDs for each entity and returns them as java.lang.String. An application entity using that would look like this:

Example 5. Mutable MovieEntity with externally generated surrogate key
@Node("Movie")
public class MovieEntity {

	@Id @GeneratedValue(UUIDStringGenerator.class)
	private String id;

	private String name;
}

We have to discuss two separate things regarding advantages and disadvantages. The assignment itself and the UUID-Strategy. A universally unique identifier is meant to be unique for practical purposes. To quote Wikipedia: “Thus, anyone can create a UUID and use it to identify something with near certainty that the identifier does not duplicate one that has already been, or will be, created to identify something else.” Our strategy uses Java internal UUID mechanism, employing a cryptographically strong pseudo random number generator. In most cases that should work fine, but your mileage might vary.

That leaves the assignment itself:

  • Advantage: The application is in full control and can generate a unique key that is just unique enough for the purpose of the application. The generated value will be stable and there won’t be a need to change it later on.

  • Disadvantage: The generated strategy is applied on the application side of things. In those days most applications will be deployed in more than one instance to scale nicely. If your strategy is prone to generate duplicates than inserts will fail as uniques of the primary key will be violated. So while you don’t have to think about a unique business key in this scenario, you have to think more what to generate.

You have several options to role your own ID generator. One is a POJO implementing a generator:

Example 6. Naive sequence generator
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.data.neo4j.core.schema.IdGenerator;
import org.springframework.util.StringUtils;

public class TestSequenceGenerator implements IdGenerator<String> {

	private final AtomicInteger sequence = new AtomicInteger(0);

	@Override
	public String generateId(String primaryLabel, Object entity) {
		return StringUtils.uncapitalize(primaryLabel) +
			"-" + sequence.incrementAndGet();
	}
}

Another option is to provide an additional Spring Bean like this:

Example 7. Neo4jClient based ID generator
@Component
class MyIdGenerator implements IdGenerator<String> {

	private final Neo4jClient neo4jClient;

	public MyIdGenerator(Neo4jClient neo4jClient) {
		this.neo4jClient = neo4jClient;
	}

	@Override
	public String generateId(String primaryLabel, Object entity) {
		return neo4jClient.query("YOUR CYPHER QUERY FOR THE NEXT ID") (1)
			.fetchAs(String.class).one().get();
	}
}
1 Use exactly the query or logic your need.

The generator above would be configured as a bean reference like this:

Example 8. Mutable MovieEntity using a Spring Bean as Id generator
@Node("Movie")
public class MovieEntity {

	@Id @GeneratedValue(generatorRef = "myIdGenerator")
	private String id;

	private String name;
}

5.2.3. Using a business key

We have been using a business key in the complete example’s MovieEntity and PersonEntity. The name of the person is assigned at construction time, both by your application and while being loaded through Spring Data.

This is only possible, if you find a stable, unique business key, but makes great immutable domain objects.

  • Advantages: Using a business or natural key as primary key is natural. The entity in question is clearly identified and it feels most of the time just right in the further modelling of your domain.

  • Disadvantages: Business keys as primary keys will be hard to update once you realise that the key you found is not as stable as you thought. Often it turns out that it can change, even when promised otherwise. Apart from that, finding identifier that are truly unique for a thing is hard.

5.3. Spring Data Object Mapping Fundamentals

This section covers the fundamentals of Spring Data object mapping, object creation, field and property access, mutability and immutability.

Core responsibility of the Spring Data object mapping is to create instances of domain objects and map the store-native data structures onto those. This means we need two fundamental steps:

  1. Instance creation by using one of the constructors exposed.

  2. Instance population to materialize all exposed properties.

5.3.1. Object creation

Spring Data automatically tries to detect a persistent entity’s constructor to be used to materialize objects of that type. The resolution algorithm works as follows:

  1. If there is a no-argument constructor, it will be used. Other constructors will be ignored.

  2. If there is a single constructor taking arguments, it will be used.

  3. If there are multiple constructors taking arguments, the one to be used by Spring Data will have to be annotated with @PersistenceConstructor.

The value resolution assumes constructor argument names to match the property names of the entity, i.e. the resolution will be performed as if the property was to be populated, including all customizations in mapping (different datastore column or field name etc.). This also requires either parameter names information available in the class file or an @ConstructorProperties annotation being present on the constructor.

Object creation internals

To avoid the overhead of reflection, Spring Data object creation uses a factory class generated at runtime by default, which will call the domain classes constructor directly. I.e. for this example type:

class Person {
  Person(String firstname, String lastname) { … }
}

we will create a factory class semantically equivalent to this one at runtime:

class PersonObjectInstantiator implements ObjectInstantiator {

  Object newInstance(Object... args) {
    return new Person((String) args[0], (String) args[1]);
  }
}

This gives us a roundabout 10% performance boost over reflection. For the domain class to be eligible for such optimization, it needs to adhere to a set of constraints:

  • it must not be a private class

  • it must not be a non-static inner class

  • it must not be a CGLib proxy class

  • the constructor to be used by Spring Data must not be private

If any of these criteria match, Spring Data will fall back to entity instantiation via reflection.

5.3.2. Property population

Once an instance of the entity has been created, Spring Data populates all remaining persistent properties of that class. Unless already populated by the entity’s constructor (i.e. consumed through its constructor argument list), the identifier property will be populated first to allow the resolution of cyclic object references. After that, all non-transient properties that have not already been populated by the constructor are set on the entity instance. For that we use the following algorithm:

  1. If the property is immutable but exposes a wither method (see below), we use the wither to create a new entity instance with the new property value.

  2. If property access (i.e. access through getters and setters) is defined, we are invoking the setter method.

  3. By default, we set the field value directly.

Property population internals

Similarly to our optimizations in object construction we also use Spring Data runtime generated accessor classes to interact with the entity instance.

class Person {

  private final Long id;
  private String firstname;
  private @AccessType(Type.PROPERTY) String lastname;

  Person() {
    this.id = null;
  }

  Person(Long id, String firstname, String lastname) {
    // Field assignments
  }

  Person withId(Long id) {
    return new Person(id, this.firstname, this.lastame);
  }

  void setLastname(String lastname) {
    this.lastname = lastname;
  }
}
Example 9. A generated Property Accessor
class PersonPropertyAccessor implements PersistentPropertyAccessor {

  private static final MethodHandle firstname;              (2)

  private Person person;                                    (1)

  public void setProperty(PersistentProperty property, Object value) {

    String name = property.getName();

    if ("firstname".equals(name)) {
      firstname.invoke(person, (String) value);             (2)
    } else if ("id".equals(name)) {
      this.person = person.withId((Long) value);            (3)
    } else if ("lastname".equals(name)) {
      this.person.setLastname((String) value);              (4)
    }
  }
}
1 PropertyAccessor’s hold a mutable instance of the underlying object. This is, to enable mutations of otherwise immutable properties.
2 By default, Spring Data uses field-access to read and write property values. As per visibility rules of private fields, MethodHandles are used to interact with fields.
3 The class exposes a withId(…) method that’s used to set the identifier, e.g. when an instance is inserted into the datastore and an identifier has been generated. Calling withId(…) creates a new Person object. All subsequent mutations will take place in the new instance leaving the previous untouched.
4 Using property-access allows direct method invocations without using MethodHandles.

This gives us a roundabout 25% performance boost over reflection. For the domain class to be eligible for such optimization, it needs to adhere to a set of constraints:

  • Types must not reside in the default or under the java package.

  • Types and their constructors must be public

  • Types that are inner classes must be static.

  • The used Java Runtime must allow for declaring classes in the originating ClassLoader. Java 9 and newer impose certain limitations.

By default, Spring Data attempts to use generated property accessors and falls back to reflection-based ones if a limitation is detected.

Let’s have a look at the following entity:

Example 10. A sample entity
class Person {

  private final @Id Long id;                                                (1)
  private final String firstname, lastname;                                 (2)
  private final LocalDate birthday;
  private final int age; (3)

  private String comment;                                                   (4)
  private @AccessType(Type.PROPERTY) String remarks;                        (5)

  static Person of(String firstname, String lastname, LocalDate birthday) { (6)

    return new Person(null, firstname, lastname, birthday,
      Period.between(birthday, LocalDate.now()).getYears());
  }

  Person(Long id, String firstname, String lastname, LocalDate birthday, int age) { (6)

    this.id = id;
    this.firstname = firstname;
    this.lastname = lastname;
    this.birthday = birthday;
    this.age = age;
  }

  Person withId(Long id) {                                                  (1)
    return new Person(id, this.firstname, this.lastname, this.birthday);
  }

  void setRemarks(String remarks) {                                         (5)
    this.remarks = remarks;
  }
}
1 The identifier property is final but set to null in the constructor. The class exposes a withId(…) method that’s used to set the identifier, e.g. when an instance is inserted into the datastore and an identifier has been generated. The original Person instance stays unchanged as a new one is created. The same pattern is usually applied for other properties that are store managed but might have to be changed for persistence operations.
2 The firstname and lastname properties are ordinary immutable properties potentially exposed through getters.
3 The age property is an immutable but derived one from the birthday property. With the design shown, the database value will trump the defaulting as Spring Data uses the only declared constructor. Even if the intent is that the calculation should be preferred, it’s important that this constructor also takes age as parameter (to potentially ignore it) as otherwise the property population step will attempt to set the age field and fail due to it being immutable and no wither being present.
4 The comment property is mutable is populated by setting its field directly.
5 The remarks properties are mutable and populated by setting the comment field directly or by invoking the setter method for
6 The class exposes a factory method and a constructor for object creation. The core idea here is to use factory methods instead of additional constructors to avoid the need for constructor disambiguation through @PersistenceConstructor. Instead, defaulting of properties is handled within the factory method.

5.3.3. General recommendations

  • Try to stick to immutable objects — Immutable objects are straightforward to create as materializing an object is then a matter of calling its constructor only. Also, this avoids your domain objects to be littered with setter methods that allow client code to manipulate the objects state. If you need those, prefer to make them package protected so that they can only be invoked by a limited amount of co-located types. Constructor-only materialization is up to 30% faster than properties population.

  • Provide an all-args constructor — Even if you cannot or don’t want to model your entities as immutable values, there’s still value in providing a constructor that takes all properties of the entity as arguments, including the mutable ones, as this allows the object mapping to skip the property population for optimal performance.

  • Use factory methods instead of overloaded constructors to avoid @PersistenceConstructor — With an all-argument constructor needed for optimal performance, we usually want to expose more application use case specific constructors that omit things like auto-generated identifiers etc. It’s an established pattern to rather use static factory methods to expose these variants of the all-args constructor.

  • Make sure you adhere to the constraints that allow the generated instantiator and property accessor classes to be used

  • For identifiers to be generated, still use a final field in combination with a wither method

  • Use Lombok to avoid boilerplate code — As persistence operations usually require a constructor taking all arguments, their declaration becomes a tedious repetition of boilerplate parameter to field assignments that can best be avoided by using Lombok’s @AllArgsConstructor.

5.3.4. Kotlin support

Spring Data adapts specifics of Kotlin to allow object creation and mutation.

Kotlin object creation

Kotlin classes are supported to be instantiated , all classes are immutable by default and require explicit property declarations to define mutable properties. Consider the following data class Person:

data class Person(val id: String, val name: String)

The class above compiles to a typical class with an explicit constructor. We can customize this class by adding another constructor and annotate it with @PersistenceConstructor to indicate a constructor preference:

data class Person(var id: String, val name: String) {

    @PersistenceConstructor
    constructor(id: String) : this(id, "unknown")
}

Kotlin supports parameter optionality by allowing default values to be used if a parameter is not provided. When Spring Data detects a constructor with parameter defaulting, then it leaves these parameters absent if the data store does not provide a value (or simply returns null) so Kotlin can apply parameter defaulting. Consider the following class that applies parameter defaulting for name

data class Person(var id: String, val name: String = "unknown")

Every time the name parameter is either not part of the result or its value is null, then the name defaults to unknown.

Property population of Kotlin data classes

In Kotlin, all classes are immutable by default and require explicit property declarations to define mutable properties. Consider the following data class Person:

data class Person(val id: String, val name: String)

This class is effectively immutable. It allows to create new instances as Kotlin generates a copy(…) method that creates new object instances copying all property values from the existing object and applying property values provided as arguments to the method.

Unresolved directive in index.adoc - include::../../../../../spring-data-commons/src/main/asciidoc/repository-projections.adoc[leveloffset=+1]

5.4. General remarks

As stated above, projections come in two flavors: Interface and DTO based projections. In Spring Data Neo4j both types of projections have a direct influence which properties and relationships are transferred over the wire. Therefore, both approaches can reduce the load on your database in case you are dealing with nodes and entities containing lots of properties which might not be needed in all usage scenarios in your application.

For both interface and DTO based projections, Spring Data Neo4j will use the repository’s domain type for building the query. All annotations on all attributes that might change the query will be taken in consideration. The domain type is the type that has been defined through the repository declaration (Given a declaration like interface TestRepository extends CrudRepository<TestEntity, Long> the domain type would be TestEntity).

Interface based projections will always be dynamic proxies to the underlying domain type. The names of the accessors defined on such interfaces (like getName) must resolve to properties (here: name) that are present on the projected entity. Whether those properties have accessors or not on the domain type is not relevant, as long as they can be accessed through the common Spring Data infrastructure. The later is ensure already, as the domain type wouldn’t be a persistent entity in the first place.

DTO based projections are somewhat more flexible when used with custom queries. While the standard query is derived from the original domain type and therefore only the properties and relationship beeing defined there can be used, custom queries can add additional properties.

The rules are as follows: First, the properties of the domain type are used to populate the DTO. In case the DTO declare additional properties - via accessors or fields - Spring Data Neo4j looks in the resulting record for matching properties. Properties must match exactly by name and can be of simple types (as defined in org.springframework.data.neo4j.core.convert.Neo4jSimpleTypes) or of known persistent entites. Collections of those are supported, but no maps.

5.4.1. A full example

Given the following entities, projections and the corresponding repository:

Listing 11. A simple entity
@Node
class TestEntity {
    @Id @GeneratedValue private Long id;

    private String name;

    @Property("a_property") (1)
    private String aProperty;
}
1 This property has a different name in the Graph
Listing 12. A derived entity, inheriting from TestEntity
@Node
class ExtendedTestEntity extends TestEntity {

    private String otherAttribute;
}
Listing 13. Interface projection of TestEntity
interface TestEntityInterfaceProjection {

    String getName();
}
Listing 14. DTO projection of TestEntity, including one additional attribute
class TestEntityDTOProjection {

    private String name;

    private Long numberOfRelations; (1)

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getNumberOfRelations() {
        return numberOfRelations;
    }

    public void setNumberOfRelations(Long numberOfRelations) {
        this.numberOfRelations = numberOfRelations;
    }
}
1 This attribute doesn’t exist on the projected entity

A repository for TestEntity is shown below and it will behave as explained with the listing.

Listing 15. A repository for the TestEntity
interface TestRepository extends CrudRepository<TestEntity, Long> { (1)

    List<TestEntity> findAll(); (2)

    List<ExtendedTestEntity> findAllExtendedEntites(); (3)

    List<TestEntityInterfaceProjection> findAllInterfaceProjections(); (4)

    List<TestEntityDTOProjection> findAllDTOProjections(); (5)

    @Query("MATCH (t:TestEntity) - [r:RELATED_TO] -> () RETURN t, COUNT(r) AS numberOfRelations") (6)
    List<TestEntityDTOProjection> findAllDTOProjectionsWithCustomQuery();
}
1 The domain type of the repository is TestEntity
2 Methods returning one or more TestEntity will just return instances of it, as it matches the domain type
3 Methods returning one or more instances of class that extend the domain type will just return instances of the extending class. The domain type of the method in question will the extended class, which still satifies the domain type of the repository itself
4 This method returns an interface projection, the return type of the method is therefore different from the repository’s domain type. The interface can only access properties defined in the domain type
5 This method returns a DTO projection. Executing it will cause SDN to issue a warning, as the DTO defines numberOfRelations as additional attribute, which is not in the contract of the domain type. The annotated attribute aProperty in TestEntity will be correctly translated to a_property in the query. As above, the return type is different from the repositories domain type.
6 This method also returns a DTO projection. However, no warning will be issued, as the query contains a fitting value for the additional attributes defined in the projection.

6. Testing

6.1. Without Spring Boot

We work a lot with our abstract base classes for configuration in our own integration tests. They can be use like this:

Listing 16. One possible test setup without Spring Boot
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.AbstractNeo4jConfig;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@ExtendWith(SpringExtension.class)
class YourIntegrationTest {

	@Test
	void thingsShouldWork(@Autowired Neo4jTemplate neo4jTemplate) {
		// Add your test
	}

	@Configuration
	@EnableNeo4jRepositories(considerNestedRepositories = true)
	@EnableTransactionManagement
	static class Config extends AbstractNeo4jConfig {

		@Bean
		public Driver driver() {
			return GraphDatabase.driver("bolt://yourtestserver:7687", AuthTokens.none()); (1)
		}
	}
}
  1. Here you should provide a connection to your test server or container.

Similar classes are provided for reactive tests.

6.2. With Spring Boot and @DataNeo4jTest

Spring Boot offers @DataNeo4jTest through org.springframework.boot:spring-boot-starter-test. The later brings in org.springframework.boot:spring-boot-test-autoconfigure which contains the annotion and the required infrastructure code.

Listing 17. Include Spring Boot Starter Test in a Maven build
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

Or with Gradle

Listing 18. Include Spring Boot Starter Test in a Gradle build
dependencies {
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

@DataNeo4jTest is a Spring Boot test slice. The test slice provides all the necessary infrastructure for tests using Neo4j: A transaction manager, a client, a template and declared repositories, in their imperative or reactive variants, depending on reactive dependencies present or not. The test slice already includes @ExtendWith(SpringExtension.class) so that it runs automatically with JUnit 5 (JUnit Jupiter).

@DataNeo4jTest provides both imperative and reactive infrastructure by default. It does not make your test @Transactional however.

@Transactional in Spring tests however always means imperative transactional, as declarative transactions needs the return type of a method to decide whether the imperative PlatformTransactionManager or the reactive ReactiveTransactionManager is needed. Testmethods however don’t return anything and neither the Spring Framework itself, the testing framework or Spring Data Neo4j can decide this in a reliable way for the user.

Make sure to add @Transactional to your test class in case you want to test imperative transactions that should be automatically rolled back after the test. To assert the correct transactional behaviour for reactive repositories or services, you will need to inject a TransactionalOperator into the test or wrap your domain logic in services that use annotated methods exposing a return type that makes it possible for the infrastructure to select the correct transaction manager.

The test slice does not bring in an embedded database or any other connection setting. It is up to you to use an appropriate connection.

We recommend one of two options: Either use the Neo4j Testcontainers module or the Neo4j test harness. While Testcontainers is a known project with modules for a lot of different services, Neo4j test harness is rather unknown. It is an embedded instance that is especially useful when testing stored procedures as described in Testing your Neo4j-based Java application. The test harness can however be used to test an application as well. As it brings up a database inside the same JVM as your application, performance and timings may not reassemble your production setup.

For your convinience we provide three possible scenarios, Neo4j test harness 3.5 and 4.0 as well as Testcontainers Neo4j. We provide different examples for 3.5 and 4.0 as the test harness changed between those versions. Also, 4.0 requires JDK 11.

6.2.1. @DataNeo4jTest with Neo4j test harness 3.5

You need the following dependencies to run Listing 20:

Listing 19. Neo4j 4.0 test harness dependencies
<dependency>
    <groupId>org.neo4j.test</groupId>
    <artifactId>neo4j-harness</artifactId>
    <version>3.5.21</version>
    <scope>test</scope>
</dependency>

The dependencies for the enterprise version of Neo4j 3.5 are available under the com.neo4j.test:neo4j-harness-enterprise and an appropriate repository configuration.

Listing 20. Using Neo4j 3.5 test harness
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Optional;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.neo4j.harness.ServerControls;
import org.neo4j.harness.TestServerBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@DataNeo4jTest
class MovieRepositoryTest {

	private static ServerControls embeddedDatabaseServer;

	@BeforeAll
	static void initializeNeo4j() {

		embeddedDatabaseServer = TestServerBuilders.newInProcessBuilder() (1)
			.newServer();
	}

	@AfterAll
	static void stopNeo4j() {

		embeddedDatabaseServer.close(); (2)
	}

	@DynamicPropertySource  (3)
	static void neo4jProperties(DynamicPropertyRegistry registry) {

		registry.add("spring.neo4j.uri", embeddedDatabaseServer::boltURI);
		registry.add("spring.neo4j.authentication.username", () -> "neo4j");
		registry.add("spring.neo4j.authentication.password", () -> null);
	}

	@Test
	public void findSomethingShouldWork(@Autowired Neo4jClient client) {

		Optional<Long> result = client.query("MATCH (n) RETURN COUNT(n)")
			.fetchAs(Long.class)
			.one();
		assertThat(result).hasValue(0L);
	}
}
1 Entrypoint to create an embedded Neo4j
2 This is a Spring Boot annotation that allows for dynamically registered application properties. We overwrite the corresponding Neo4j settings.
3 Shutdown Neo4j after all tests.

6.2.2. @DataNeo4jTest with Neo4j test harness 4.0+

You need the following dependencies to run Listing 22:

Listing 21. Neo4j 4.0 test harness dependencies
<dependency>
    <groupId>org.neo4j.test</groupId>
    <artifactId>neo4j-harness</artifactId>
    <version>4.0.8</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
        </exclusion>
    </exclusions>
</dependency>

The dependencies for the enterprise version of Neo4j 4.x are available under the com.neo4j.test:neo4j-harness-enterprise and an appropriate repository configuration.

Listing 22. Using Neo4j 4.0+ test harness
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Optional;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.neo4j.harness.Neo4j;
import org.neo4j.harness.Neo4jBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@DataNeo4jTest
class MovieRepositoryTest {

	private static Neo4j embeddedDatabaseServer;

	@BeforeAll
	static void initializeNeo4j() {

		embeddedDatabaseServer = Neo4jBuilders.newInProcessBuilder() (1)
			.withDisabledServer() (2)
			.build();
	}

	@DynamicPropertySource (3)
	static void neo4jProperties(DynamicPropertyRegistry registry) {

		registry.add("spring.neo4j.uri", embeddedDatabaseServer::boltURI);
		registry.add("spring.neo4j.authentication.username", () -> "neo4j");
		registry.add("spring.neo4j.authentication.password", () -> null);
	}

	@AfterAll
	static void stopNeo4j() {

		embeddedDatabaseServer.close(); (4)
	}

	@Test
	public void findSomethingShouldWork(@Autowired Neo4jClient client) {

		Optional<Long> result = client.query("MATCH (n) RETURN COUNT(n)")
			.fetchAs(Long.class)
			.one();
		assertThat(result).hasValue(0L);
	}
}
1 Entrypoint to create an embedded Neo4j
2 Don’t need Neoj4’s HTTP server
3 This is a Spring Boot annotation that allows for dynamically registered application properties. We overwrite the corresponding Neo4j settings.
4 Shutdown Neo4j after all tests.

6.2.3. @DataNeo4jTest with Testcontainers Neo4j

The principal of configuring the connection is of course still the same with Testcontainers as shown in Listing 23. You need the following dependencies:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>neo4j</artifactId>
    <version>1.14.2</version>
    <scope>test</scope>
</dependency>

And a complete test:

Listing 23. Using Test containers
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Optional;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.Neo4jContainer;

@DataNeo4jTest
class MovieRepositoryTCTest {

	private static Neo4jContainer<?> neo4jContainer;

	@BeforeAll
	static void initializeNeo4j() {

		neo4jContainer = new Neo4jContainer<>()
			.withAdminPassword("somePassword");
		neo4jContainer.start();
	}

	@AfterAll
	static void stopNeo4j() {

		neo4jContainer.close();
	}

	@DynamicPropertySource
	static void neo4jProperties(DynamicPropertyRegistry registry) {

		registry.add("spring.neo4j.uri", neo4jContainer::getBoltUrl);
		registry.add("spring.neo4j.authentication.username", () -> "neo4j");
		registry.add("spring.neo4j.authentication.password", neo4jContainer::getAdminPassword);
	}

	@Test
	public void findSomethingShouldWork(@Autowired Neo4jClient client) {

		Optional<Long> result = client.query("MATCH (n) RETURN COUNT(n)")
			.fetchAs(Long.class)
			.one();
		assertThat(result).hasValue(0L);
	}
}

6.2.4. Alternatives to a @DynamicPropertySource

There are some scenarios in which the above annotation does not fit your usecase. One of those might be that you want to have 100% control over how the driver is initialized. With a test container running, you could do this with a nested, static configuration class like this:

@TestConfiguration(proxyBeanMethods = false)
static class TestNeo4jConfig {

    @Bean
    Driver driver() {
        return GraphDatabase.driver(
        		neo4jContainer.getBoltUrl(),
        		AuthTokens.basic("neo4j", neo4jContainer.getAdminPassword())
        );
    }
}

If you want to use the properties but cannot use a @DynamicPropertySource, you would use an initializer:

Listing 24. Alternative injection of dynamic properties
@ContextConfiguration(initializers = PriorToBoot226Test.Initializer.class)
@DataNeo4jTest
class PriorToBoot226Test {

    private static Neo4jContainer<?> neo4jContainer;

    @BeforeAll
    static void initializeNeo4j() {

        neo4jContainer = new Neo4jContainer<>()
            .withAdminPassword("somePassword");
        neo4jContainer.start();
    }

    @AfterAll
    static void stopNeo4j() {

        neo4jContainer.close();
    }

    static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            TestPropertyValues.of(
                "spring.neo4j.uri=" + neo4jContainer.getBoltUrl(),
                "spring.neo4j.authentication.username=neo4j",
                "spring.neo4j.authentication.password=" + neo4jContainer.getAdminPassword()
            ).applyTo(configurableApplicationContext.getEnvironment());
        }
    }
}

Unresolved directive in index.adoc - include::../../../../../spring-data-commons/src/main/asciidoc/auditing.adoc[leveloffset=+1]

7. Frequently Asked Questions

Here are a couple of more frequently asked question in addition to the ones in the preface.

7.1. Neo4j 4.0 supports multiple databases - How can I use them?

You can either statically configure the database name or run your own database name provider. Bear in mind that SDN will not create the databases for you. You can do this with the help of a migrations tool or of course with a simple script upfront.

7.1.1. Statically configured

Configure the database name to use in your Spring Boot configuration like this (The same property applies of course for YML or environment based configuration, with Spring Boots conventions applied):

org.neo4j.data.database = yourDatabase

With that configuration in place, all queries generated by all instances of SDN repositories (both reactive and imperative) and by the ReactiveNeo4jTemplate respectively Neo4jTemplate will be executed against the database yourDatabase.

7.1.2. Dynamically configured

Provide a bean with the type Neo4jDatabaseNameProvider or ReactiveDatabaseSelectionProvider depending on the type of your Spring application.

That bean could use for example Spring’s security context to retrieve a tenant. Here is a working example for an imperative application secured with Spring Security:

Listing 25. Neo4jConfig.java
import org.neo4j.springframework.data.core.DatabaseSelection;
import org.neo4j.springframework.data.core.DatabaseSelectionProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

@Configuration
public class Neo4jConfig {

	@Bean
	DatabaseSelectionProvider databaseSelectionProvider() {

		return () -> Optional.ofNullable(SecurityContextHolder.getContext()).map(SecurityContext::getAuthentication)
				.filter(Authentication::isAuthenticated).map(Authentication::getPrincipal).map(User.class::cast)
				.map(User::getUsername).map(DatabaseSelection::byName).orElseGet(DatabaseSelection::undecided);
	}
}
Be careful that you don’t mix up entities retrieved from one database with another database. The database name is requested for each new transaction, so you might end up with less or more entities than expected when changing the database name in between calls. Or worse, you could inevitable store the wrong entities in the wrong database.

7.2. Do I need specific configuration so that transactions work seamless with a Neo4j Causal Cluster?

No, you don’t. SDN uses Neo4j Causal Cluster bookmarks internally without any configuration on your side required. Transactions in the same thread or the same reactive stream following each other will be able to read their previously changed values as you would expect.

7.3. Do I need to use Neo4j specific annotations?

No. You are free to use the following, equivalent Spring Data annotations:

SDN specific annotation Spring Data common annotation Purpose Difference

org.springframework.data.neo4j.core.schema.Id

org.springframework.data.annotation.Id

Marks the annotated attribute as the unique id.

Specific annotation has no additional features.

org.springframework.data.neo4j.core.schema.Node

org.springframework.data.annotation.Persistent

Marks the class as persistent entity.

@Node allows customizing the labels

7.4. How do I use assigned ids?

Just @Id without @GeneratedValue and fill your id attribute via a constructor parameter or a setter or wither. See this blog post for some general remarks about finding good ids.

7.5. How do I use externally generated ids?

We provide the interface org.springframework.data.neo4j.core.schema.IdGenerator. Implement it anyway you want and configure your implementation like this:

Listing 26. ThingWithGeneratedId.java
@Node
public class ThingWithGeneratedId {

	@Id @GeneratedValue(TestSequenceGenerator.class)
	private String theId;
}

If you pass in the name of a class to @GeneratedValue, this class must have a no-args default constructor. You can however use a string as well:

Listing 27. ThingWithIdGeneratedByBean.java
@Node
public class ThingWithIdGeneratedByBean {

	@Id @GeneratedValue(generatorRef = "idGeneratingBean")
	private String theId;
}

With that, idGeneratingBean refers to a bean in the Spring context. This might be useful for sequence generating.

Setters are not required on non-final fields for the id.

7.6. Do I have to create repositories for each domain class?

No. Have a look at the SDN building blocks and find the Neo4jTemplate respectively the ReactiveNeo4jTemplate.

Those templates know your domain and provide all necessary basic CRUD methods for retrieving, writing and counting entities.

This is our canonical movie example with the imperative template:

Listing 28. TemplateExampleTest.java
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Collections;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import org.springframework.data.neo4j.documentation.Test;
import org.springframework.data.neo4j.documentation.domain.MovieEntity;
import org.springframework.data.neo4j.documentation.domain.PersonEntity;
import org.springframework.data.neo4j.documentation.domain.Roles;

@DataNeo4jTest
public class TemplateExampleTest {
	@Test
	void shouldSaveAndReadEntities(@Autowired Neo4jTemplate neo4jTemplate) {

		MovieEntity movie = new MovieEntity("The Love Bug",
				"A movie that follows the adventures of Herbie, Herbie's driver, "
						+ "Jim Douglas (Dean Jones), and Jim's love interest, " + "Carole Bennett (Michele Lee)");

		movie.getActorsAndRoles().put(new PersonEntity(1931, "Dean Jones"), new Roles(Collections.singletonList("Didi")));
		movie.getActorsAndRoles().put(new PersonEntity(1942, "Michele Lee"), new Roles(Collections.singletonList("Michi")));

		neo4jTemplate.save(movie);

		Optional<PersonEntity> person = neo4jTemplate.findById("Dean Jones", PersonEntity.class);
		assertThat(person).map(PersonEntity::getBorn).hasValue(1931);

		assertThat(neo4jTemplate.count(PersonEntity.class)).isEqualTo(2L);
	}
}

And here is the reactive version, omitting the setup for brevity:

Listing 29. ReactiveTemplateExampleTest.java
import reactor.test.StepVerifier;

import java.util.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.core.ReactiveNeo4jTemplate;
import org.springframework.data.neo4j.documentation.Test;
import org.springframework.data.neo4j.documentation.domain.MovieEntity;
import org.springframework.data.neo4j.documentation.domain.PersonEntity;
import org.springframework.data.neo4j.documentation.domain.Roles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
@DataNeo4jTest
class ReactiveTemplateExampleTest {

	@Container private static Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:4.0");

	@DynamicPropertySource
	static void neo4jProperties(DynamicPropertyRegistry registry) {
		registry.add("org.neo4j.driver.uri", neo4jContainer::getBoltUrl);
		registry.add("org.neo4j.driver.authentication.username", () -> "neo4j");
		registry.add("org.neo4j.driver.authentication.password", neo4jContainer::getAdminPassword);
	}

	@Test
	void shouldSaveAndReadEntities(@Autowired ReactiveNeo4jTemplate neo4jTemplate) {

		MovieEntity movie = new MovieEntity("The Love Bug",
				"A movie that follows the adventures of Herbie, Herbie's driver, "
						+ "Jim Douglas (Dean Jones), and Jim's love interest, " + "Carole Bennett (Michele Lee)");

		movie.getActorsAndRoles().put(new PersonEntity(1931, "Dean Jones"), new Roles(Collections.singletonList("Didi")));
		movie.getActorsAndRoles().put(new PersonEntity(1942, "Michele Lee"), new Roles(Collections.singletonList("Michi")));

		StepVerifier.create(neo4jTemplate.save(movie)).expectNextCount(1L).verifyComplete();

		StepVerifier.create(neo4jTemplate.findById("Dean Jones", PersonEntity.class).map(PersonEntity::getBorn))
				.expectNext(1931).verifyComplete();

		StepVerifier.create(neo4jTemplate.count(PersonEntity.class)).expectNext(2L).verifyComplete();
	}
}

7.7. How do I specify parameters in custom queries?

You do this exactly the same way as in a standard Cypher query issued in the Neo4j Browser or the Cypher-Shell, with the $ syntax (from Neo4j 4.0 on upwards, the old {foo} syntax for Cypher parameters has been removed from the database);

Listing 30. ARepository.java
public interface ARepository extends Neo4jRepository<AnAggregateRoot, String> {

	@Query("MATCH (a:AnAggregateRoot {name: $name}) RETURN a") (1)
	Optional<AnAggregateRoot> findByCustomQuery(String name);
}
1 Here we are referring to the parameter by its name. You can also use $0 etc. instead.
You need to compile your Java 8+ project with -parameters to make named parameters work without further annotations. The Spring Boot Maven and Gradle plugins do this automatically for you. If this is not feasible for any reason, you can either add @Param and specify the name explicitly or use the parameters index.

7.8. How do I use Spring Expression Language in custom queries?

Spring Expression Language (SpEL) can be used in custom queries inside :#{}. This is the standard Spring Data way of defining a block of text inside a query that undergoes SpEL evaluation.

The following example basically defines the same query as above, but uses a WHERE clause to avoid even more curly braces:

Listing 31. ARepository.java
public interface ARepository extends Neo4jRepository<AnAggregateRoot, String> {

	@Query("MATCH (a:AnAggregateRoot) WHERE a.name = :#{#pt1 + #pt2} RETURN a")
	Optional<AnAggregateRoot> findByCustomQueryWithSpEL(String pt1, String pt2);
}

The SpEL blocked starts with :#{ and than refers to the given String parameters by name (#pt1). Don’t confuse this with the above Cypher syntax! The SpEL expression concatenates both parameters into one single value that is eventually passed on to the Neo4jClient. The SpEL block ends with }.

7.9. How do I audit entities?

All Spring Data annotations are supported. Those are

  • org.springframework.data.annotation.CreatedBy

  • org.springframework.data.annotation.CreatedDate

  • org.springframework.data.annotation.LastModifiedBy

  • org.springframework.data.annotation.LastModifiedDate

7.10. How do I use "Find by example"?

"Find by example" is a new feature in SDN. You instantiate an entity or use an existing one. With this instance you create an org.springframework.data.domain.Example. If your repository extends org.springframework.data.neo4j.repository.Neo4jRepository or org.springframework.data.neo4j.repository.ReactiveNeo4jRepository, you can immediately use the available findBy methods taking in an example, like shown in Listing 32

Listing 32. findByExample in Action
Example<MovieEntity> movieExample = Example.of(new MovieEntity("The Matrix", null));
Flux<MovieEntity> movies = this.movieRepository.findAll(movieExample);

movieExample = Example.of(
	new MovieEntity("Matrix", null),
	ExampleMatcher
	    .matchingAny()
        .withMatcher(
        	"title",
        	ExampleMatcher.GenericPropertyMatcher.of(ExampleMatcher.StringMatcher.CONTAINING)
        )
);
movies = this.movieRepository.findAll(movieExample);

7.11. Do I need Spring Boot to use Spring Data Neo4j?

No, you don’t. While the automatic configuration of many Spring aspects through Spring Boot takes away a lot of manual cruft and is the recommended approach for setting up new Spring projects, you don’t need to have to use this.

The following dependency is required for the solutions described above:

<dependency>
	<groupId>{springGroupId}</groupId>
	<artifactId>spring-data-neo4j</artifactId>
	<version>6.0.0-SNAPSHOT</version>
</dependency>

The coordinates for a Gradle setup are the same.

To select a different database - either statically or dynamically - you can add a Bean of type DatabaseSelectionProvider as explained in Section 7.1. For a reactive scenario, we provide ReactiveDatabaseSelectionProvider.

7.11.1. Using Spring Data Neo4j inside a Spring context without Spring Boot

We provide two abstract configuration classes to support you in bringing in the necessary beans: AbstractNeo4jConfig for imperative database access and AbstractReactiveNeo4jConfig for the reactive version. They are meant to be used with @EnableNeo4jRepositories and @EnableReactiveNeo4jRepositories respectively. See Listing 33 and Listing 34 for an example usage. Both classes require you to override driver() in which you are supposed to create the driver.

To get the imperative version of the Neo4j client, the template and support for imperative repositories, use something similar as shown here:

Listing 33. Enabling Spring Data Neo4j infrastructure for imperative database access
import org.neo4j.driver.Driver;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.transaction.annotation.EnableTransactionManagement;

import org.springframework.data.neo4j.config.AbstractNeo4jConfig;
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;

@Configuration
@EnableNeo4jRepositories
@EnableTransactionManagement
class MyConfiguration extends AbstractNeo4jConfig {

    @Override @Bean
    public Driver driver() { (1)
        return GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "secret"));
    }

    @Override
    protected Collection<String> getMappingBasePackages() {
        return Collections.singletonList(Person.class.getPackage().getName());
    }

    @Override @Bean (2)
    protected DatabaseSelectionProvider databaseSelectionProvider() {

        return DatabaseSelectionProvider.createStaticDatabaseSelectionProvider("yourDatabase");
    }
}
1 The driver bean is required.
2 This statically selects a database named yourDatabase and is optional.

The following listing provides the reactive Neo4j client and template, enables reactive transaction management and discovers Neo4j related repositories:

Listing 34. Enabling Spring Data Neo4j infrastructure for reactive database access
import org.neo4j.driver.Driver;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.AbstractReactiveNeo4jConfig;
import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableReactiveNeo4jRepositories
@EnableTransactionManagement
class MyConfiguration extends AbstractReactiveNeo4jConfig {

    @Bean
    @Override
    public Driver driver() {
        return GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "secret"));
    }

    @Override
    protected Collection<String> getMappingBasePackages() {
        return Collections.singletonList(Person.class.getPackage().getName());
    }
}

7.11.2. Using Spring Data Neo4j in a CDI 2.0 environment

For your convenience we provide a CDI extension with Neo4jCdiExtension. When run in a compatible CDI 2.0 container, it will be automatically be registered and loaded through Java’s service loader SPI.

The only thing you have to bring into your application is an annotated type that produces the Neo4j Java Driver:

Listing 35. A CDI producer for the Neo4j Java Driver
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;

public class Neo4jConfig {

    @Produces @ApplicationScoped
    public Driver driver() { (1)
        return GraphDatabase
            .driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "secret"));
    }

    public void close(@Disposes Driver driver) {
        driver.close();
    }

    @Produces @Singleton
    public DatabaseSelectionProvider getDatabaseSelectionProvider() { (2)
        return DatabaseSelectionProvider.createStaticDatabaseSelectionProvider("yourDatabase");
    }
}
1 Same as with plain Spring in Listing 33, but annotated with the corresponding CDI infrastructure.
2 This is optional. However, if you run a custom database selection provider, you must not qualify this bean.

If you are running in a SE Container - like the one Weld provides for example, you can enable the extension like that:

Listing 36. Enabling the Neo4j CDI extension in a SE container
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;

import org.springframework.data.neo4j.config.Neo4jCdiExtension;

public class SomeClass {
    void someMethod() {
        try (SeContainer container = SeContainerInitializer.newInstance()
                .disableDiscovery()
                .addExtensions(Neo4jCdiExtension.class)
                .addBeanClasses(YourDriverFactory.class)
                .addPackages(Package.getPackage("your.domain.package"))
            .initialize()
        ) {
            SomeRepository someRepository = container.select(SomeRepository.class).get();
        }
    }
}

8. Spring Data Neo4j Appendix

Conversions

We support a broad range of conversions out of the box. Find the list of supported cypher types in the official drivers manual: Working with Cypher values.

Primitive types of wrapper types are equally supported.

Domain type Cypher type Maps directly to native type

java.lang.Boolean

Boolean

boolean[]

List of Boolean

java.lang.Long

Integer

long[]

List of Integer

java.lang.Double

Float

java.lang.String

String

java.lang.String[]

List of String

byte[]

ByteArray

java.lang.Byte

ByteArray with length 1

java.lang.Character

String with length 1

char[]

List of String with length 1

java.util.Date

String formatted as ISO 8601 Date (yyyy-MM-dd’T’HH:mm:ss.SSSZ). Notice the Z: SDN will store all java.util.Date instances in UTC. If you require the time zone, use a type that supports it (i.e. ZoneDateTime) or store the zone as a separate property.

double[]

List of Float

java.lang.Float

String

float[]

List of String

java.lang.Integer

Integer

int[]

List of Integer

java.util.Locale

String formatted as BCP 47 language tag

java.lang.Short

Integer

short[]

List of Integer

java.math.BigDecimal

String

java.math.BigInteger

String

java.time.LocalDate

Date

java.time.OffsetTime

Time

java.time.LocalTime

LocalTime

java.time.ZonedDateTime

DateTime

java.time.LocalDateTime

LocalDateTime

java.time.Period

Duration

java.time.Duration

Duration

org.neo4j.driver.types.IsoDuration

Duration

org.neo4j.driver.types.Point

Point

org.springframework.data.neo4j.types.GeographicPoint2d

Point with CRS 4326

org.springframework.data.neo4j.types.GeographicPoint3d

Point with CRS 4979

org.springframework.data.neo4j.types.CartesianPoint2d

Point with CRS 7203

org.springframework.data.neo4j.types.CartesianPoint3d

Point with CRS 9157

org.springframework.data.geo.Point

Point with CRS 4326 and x/y corresponding to lat/long

Instances of Enum

String (The name value of the enum)

Instances of Enum[]

List of String (The name value of the enum)

java.net.URL

String

java.net.URI

String

Custom conversions

For attributes of a given type

If you prefer to work with your own types in the entities or as parameters for @Query annotated methods, you can define and provide a custom converter implementation. First you have to implement a GenericConverter and register the types your converter should handle. For entity property type converters you need to take care of converting your type to and from a Neo4j Java Driver Value. If your converter is supposed to work only with custom query methods in the repositories, it is sufficient to provide the one-way conversion to the Value type.

Listing 37. Example of a custom converter implementation
public class MyCustomTypeConverter implements GenericConverter {

	@Override
	public Set<ConvertiblePair> getConvertibleTypes() {
		Set<ConvertiblePair> convertiblePairs = new HashSet<>();
		convertiblePairs.add(new ConvertiblePair(MyCustomType.class, Value.class));
		convertiblePairs.add(new ConvertiblePair(Value.class, MyCustomType.class));
		return convertiblePairs;
	}

	@Override
	public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
		if (MyCustomType.class.isAssignableFrom(sourceType.getType())) {
			// convert to Neo4j Driver Value
			return convertToNeo4jValue(source);
		} else {
			// convert to MyCustomType
			return convertToMyCustomType(source);
		}
	}

}

To make SDN aware of your converter, it has to be registered in the Neo4jConversions. To do this, you have to create a @Bean with the type org.springframework.data.neo4j.core.convert.Neo4jConversions. Otherwise, the Neo4jConversions will get created in the background with the internal default converters only.

Listing 38. Example of a custom converter implementation
@Bean
public Neo4jConversions neo4jConversions() {
	Set<GenericConverter> additionalConverters = Collections.singleton(new MyCustomTypeConverter());
	return new Neo4jConversions(additionalConverters);
}

If you need multiple converters in your application, you can add as many as you need in the Neo4jConversions constructor.

For specific attributes only

If you need conversions only for some specific attributes, we provide @ConvertWith. This is an annotation that can be put on attributes carrying a Neo4jPersistentPropertyConverter on it’s converter attribute and an optional Neo4jPersistentPropertyConverterFactory to construct the former. With an implementation of Neo4jPersistentPropertyConverter all specific conversions for a given type can be addressed.

We provide @DateLong and @DateString as meta-annotated annotations for backward compatibility with Neo4j-OGM schemes not using native types.

Composite properties

With @CompositeProperty, attributes of type Map<String, Object> or Map<? extends Enum, Object>` can be stored as composite properties. All entries inside the map will be added as properties to the node or relationship containing the property. Either with a configured prefix or prefixed with the name of the property. While we only offer that feature for maps out of the box, you can Neo4jPersistentPropertyToMapConverter and configure it as the converter to use on @CompositeProperty. A Neo4jPersistentPropertyToMapConverter needs to know how a given type can be decomposed to and composed back from a map.

Neo4jClient

Spring Data Neo4j comes with a Neo4j Client, providing a thin layer on top of Neo4j’s Java driver.

While the plain Java driver is a very versatile tool providing an asynchronous API in addition to the imperative and reactive versions, it doesn’t integrate with Spring application level transactions.

SDN uses the driver through the concept of a idiomatic client as directly as possible.

The client has the following main goals

  1. Integrate into Springs transaction management, for both imperative and reactive scenarios

  2. Participate in JTA-Transactions if necessary

  3. Provide a consistent API for both imperative and reactive scenarios

  4. Don’t add any mapping overhead

SDN relies on all those features and uses them to fulfill its entity mapping features.

Have a look at the SDN building blocks for where both the imperative and reactive Neo4 clients are positioned in our stack.

The Neo4j Client comes in two flavors:

  • org.springframework.data.neo4j.core.Neo4jClient

  • org.springframework.data.neo4j.core.ReactiveNeo4jClient

While both versions provide an API using the same vocabulary and syntax, they are not API compatible. Both versions feature the same, fluent API to specify queries, bind parameters and extract results.

Imperative or reactive?

Interactions with a Neo4j Client usually ends with a call to

  • fetch().one()

  • fetch().first()

  • fetch().all()

  • run()

The imperative version will interact at this moment with the database and get the requested results or summary, wrapped in a Optional<> or a Collection.

The reactive version will in contrast return a publisher of the requested type. Interaction with the database and retrieval of the results will not happen until the publisher is subscribed to. The publisher can only be subscribed once.

Getting an instance of the client

As with most things in SDN, both clients depend on a configured driver instance.

Listing 39. Creating an instance of the imperative Neo4j client
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;

import org.springframework.data.neo4j.core.Neo4jClient;

public class Demo {

    public static void main(String...args) {

        Driver driver = GraphDatabase
            .driver("neo4j://localhost:7687", AuthTokens.basic("neo4j", "secret"));

        Neo4jClient client = Neo4jClient.create(driver);
    }
}

The driver can only open a reactive session against a 4.0 database and will fail with an exception on any lower version.

Listing 40. Creating an instance of the reactive Neo4j client
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;

import org.springframework.data.neo4j.core.ReactiveNeo4jClient;

public class Demo {

    public static void main(String...args) {

        Driver driver = GraphDatabase
            .driver("neo4j://localhost:7687", AuthTokens.basic("neo4j", "secret"));

        ReactiveNeo4jClient client = ReactiveNeo4jClient.create(driver);
    }
}
Make sure you use the same driver instance for the client as you used for providing a Neo4jTransactionManager or ReactiveNeo4jTransactionManager in case you have enabled transactions. The client won’t be able to synchronize transactions if you use another instance of a driver.

Our Spring Boot starter provide a ready to use bean of the Neo4j Client that fit the environment (imperative or reactive) and you usually don’t have to configure your own instance.

Usage

Selecting the target database

The Neo4j client is well prepared to be used with the multidatabase features of Neo4j 4.0. The client uses the default database unless you specify otherwise. The fluent API of the client allows to specify the target database exactly once, after the declaration of the query to execute. Listing 41 demonstrates it with the reactive client:

Listing 41. Selecting the target database
Flux<Map<String, Object>> allActors = client
	.query("MATCH (p:Person) RETURN p")
	.in("neo4j") (1)
	.fetch()
	.all();
1 Select the target database in which the query is to be executed.
Specifying queries

The interaction with the clients starts with a query. A query can be defined by a plain String or a Supplier<String>. The supplier will be evaluated as late as possible and can be provided by any query builder.

Listing 42. Specifying a query
Mono<Map<String, Object>> firstActor = client
	.query(() -> "MATCH (p:Person) RETURN p")
	.fetch()
	.first();
Retrieving results

As the previous listings shows, the interaction with the client always ends with a call to fetch and how many results shall be received. Both reactive and imperative client offer

one()

Expect exactly one result from the query

first()

Expect results and return the first record

all()

Retrieve all records returned

The imperative client returns Optional<T> and Collection<T> respectively, while the reactive client returns Mono<T> and Flux<T>, the later one being executed only if subscribed to.

If you don’t expect any results from your query, than use run() after specificity the query.

Listing 43. Retrieving result summaries in a reactive way
Mono<ResultSummary> summary = reactiveClient
    .query("MATCH (m:Movie) where m.title = 'Aeon Flux' DETACH DELETE m")
    .run();

summary
    .map(ResultSummary::counters)
    .subscribe(counters ->
        System.out.println(counters.nodesDeleted() + " nodes have been deleted")
    ); (1)
1 The actual query is triggered here by subscribing to the publisher.

Please take a moment to compare both listings and understand the difference when the actual query is triggered.

Listing 44. Retrieving result summaries in a imperative way
ResultSummary resultSummary = imperativeClient
	.query("MATCH (m:Movie) where m.title = 'Aeon Flux' DETACH DELETE m")
	.run(); (1)

SummaryCounters counters = resultSummary.counters();
System.out.println(counters.nodesDeleted() + " nodes have been deleted")
1 Here the query is triggered immediate.
Mapping parameters

Queries can contain named parameters ($someName). The Neo4j client allows comfortable binding to those.

The client doesn’t check whether all parameters are bound or whether there are to many values. That is left to the driver. However the client prevents you from using a parameter name twice.

You can either map simple types that the Java driver understands or complex classes. Please have a look at the drivers manual, to see which simple types are understood.

Listing 45. Mapping simple types
Map<String, Object> parameters = new HashMap<>();
parameters.put("name", "Li.*");

Flux<Map<String, Object>> directorAndMovies = client
	.query(
		"MATCH (p:Person) - [:DIRECTED] -> (m:Movie {title: $title}), (p) - [:WROTE] -> (om:Movie) " +
			"WHERE p.name =~ $name " +
			"  AND p.born < $someDate.year " +
			"RETURN p, om"
	)
	.bind("The Matrix").to("title") (1)
	.bind(LocalDate.of(1979, 9, 21)).to("someDate")
	.bindAll(parameters) (2)
	.fetch()
	.all();
1 There’s a fluent API for binding simple types.
2 Alternatively parameters can be bound via a map of named parameters.

SDN does a lot of complex mapping and it uses the same API that you can use from the client.

You can provide a Function<T, Map<String, Object>> for any given domain object like an owner of bicycles in Listing 46 to the Neo4j Client to map those domain objects to parameters the driver can understand.

Listing 46. Example of a domain type
public class Director {

    private final String name;

    private final List<Movie> movies;

    Director(String name, List<Movie> movies) {
        this.name = name;
        this.movies = new ArrayList<>(movies);
    }

    public String getName() {
        return name;
    }

    public List<Movie> getMovies() {
        return Collections.unmodifiableList(movies);
    }
}

public class Movie {

    private final String title;

    public Movie(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}

The mapping function has to fill in all named parameters that might occur in the query like Listing 47 shows:

Listing 47. Using a mapping function for binding domain objects
Director joseph = new Director("Joseph Kosinski",
        Arrays.asList(new Movie("Tron Legacy"), new Movie("Top Gun: Maverick")));

Mono<ResultSummary> summary = client
    .query(""
        + "MERGE (p:Person {name: $name}) "
        + "WITH p UNWIND $movies as movie "
        + "MERGE (m:Movie {title: movie}) "
        + "MERGE (p) - [o:DIRECTED] -> (m) "
    )
    .bind(joseph).with(director -> { (1)
        Map<String, Object> mappedValues = new HashMap<>();
        List<String> movies = director.getMovies().stream()
            .map(Movie::getTitle).collect(Collectors.toList());
        mappedValues.put("name", director.getName());
        mappedValues.put("movies", movies);
        return mappedValues;
    })
    .run();
1 The with method allows for specifying the binder function.
Working with result objects

Both clients return collections or publishers of maps (Map<String, Object>). Those maps corresponds exactly with the records that a query might have produced.

In addition, you can plugin your own BiFunction<TypeSystem, Record, T> through fetchAs to reproduce your domain object.

Listing 48. Using a mapping function for reading domain objects
Mono<Director> lily = client
    .query(""
        + " MATCH (p:Person {name: $name}) - [:DIRECTED] -> (m:Movie)"
        + "RETURN p, collect(m) as movies")
    .bind("Lilly Wachowski").to("name")
    .fetchAs(Director.class).mappedBy((TypeSystem t, Record record) -> {
        List<Movie> movies = record.get("movies")
            .asList(v -> new Movie((v.get("title").asString())));
        return new Director(record.get("name").asString(), movies);
    })
    .one();

TypeSystem gives access to the types the underlying Java driver used to fill the record.

Interacting directly with the driver while using managed transactions

In case you don’t want or don’t like the opinionated "client" approach of the Neo4jClient or the ReactiveNeo4jClient, you can have the client delegate all interactions with the database to your code. The interaction after the delegation is slightly different with the imperative and reactive versions of the client.

The imperative version takes in a Function<StatementRunner, Optional<T>> as a callback. Returning an empty optional is ok.

Listing 49. Delegate database interaction to an imperative StatementRunner
Optional<Long> result = client
    .delegateTo((StatementRunner runner) -> {
        // Do as many interactions as you want
        long numberOfNodes = runner.run("MATCH (n) RETURN count(n) as cnt")
            .single().get("cnt").asLong();
        return Optional.of(numberOfNodes);
    })
    // .in("aDatabase") (1)
    .run();
1 The database selection as described in Selecting the target database is optional.

The reactive version receives a RxStatementRunner.

Listing 50. Delegate database interaction to a reactive RxStatementRunner
Mono<Integer> result = client
    .delegateTo((RxStatementRunner runner) ->
        Mono.from(runner.run("MATCH (n:Unused) DELETE n").summary())
            .map(ResultSummary::counters)
            .map(SummaryCounters::nodesDeleted))
    // .in("aDatabase") (1)
    .run();
1 Optional selection of the target database.

Note that in both Listing 49 and Listing 50 the types of the runner have only been stated to provide more clarity to reader of this manual.

Query creation

This chapter is about the technical creation of queries when using SDN’s abstraction layers. There will be some simplifications because we do not discuss every possible case but stick with the general idea behind it.

Save

Beside the find/load operations the save operation is one of the most used when working with data. A save operation call in general issues multiple statements against the database to ensure that the resulting graph model matches the given Java model.

  1. A union statement will get created that either creates a node, if the node’s identifier cannot be found, or updates the node’s property if the node itself exists.

    (OPTIONAL MATCH (hlp:Person) WHERE id(hlp) = $__id__ WITH hlp WHERE hlp IS NULL CREATE (n:Person) SET n = $__properties__ RETURN id(n) UNION MATCH (n) WHERE id(n) = $__id__ SET n = $__properties__ RETURN id(n))

  2. If the entity is not new all relationships of the first found type at the domain model will get removed from the database.

    (MATCH (startNode)-[rel:Has]→(:Hobby) WHERE id(startNode) = $fromId DELETE rel)

  3. The related entity will get created in the same way as the root entity.

    (OPTIONAL MATCH (hlp:Hobby) WHERE id(hlp) = $__id__ WITH hlp WHERE hlp IS NULL CREATE (n:Hobby) SET n = $__properties__ RETURN id(n) UNION MATCH (n) WHERE id(n) = $__id__ SET n = $__properties__ RETURN id(n))

  4. The relationship itself will get created

    (MATCH (startNode) WHERE id(startNode) = $fromId MATCH (endNode) WHERE id(endNode) = 631 MERGE (startNode)-[:Has]→(endNode))

  5. If the related entity also has relationships to other entities, the same procedure as in 2. will get started.

  6. For the next defined relationship on the root entity start with 2. but replace first with next.

As you can see SDN does its best to keep your graph model in sync with the Java world. This is one of the reasons why we really advise you to not load, manipulate and save sub-graphs as this might cause relationships to get removed from the database.
Multiple entities

The save operation is overloaded with the functionality for accepting multiple entities of the same type. If you are working with generated id values or make use of optimistic locking, every entity will result in a separate CREATE call.

In other cases SDN will create a parameter list with the entity information and provide it with a MERGE call.

UNWIND $__entities__ AS entity MERGE (n:Person {customId: entity.$__id__}) SET n = entity.__properties__ RETURN collect(n.customId) AS $__ids__

and the parameters look like

:params {__entities__: [{__id__: 'aa', __properties__: {name: "PersonName", theId: "aa"}}, {__id__ 'bb', __properties__: {name: "AnotherPersonName", theId: "bb"}}]}

Load

The load documentation will not only show you how the MATCH part of the query looks like but also how the data gets returned.

The simplest kind of load operation is a findById call. It will match all nodes with the label of the type you queried for and does a filter on the id value.

MATCH (n:Person) WHERE id(n) = 1364

If there is a custom id provided SDN will use the property you have defined as the id.

MATCH (n:Person) WHERE n.customId = 'anId'

The data to return is defined as a map projection.

RETURN n{.first_name, .personNumber, __internalNeo4jId__: id(n), __nodeLabels__: labels(n)}

As you can see there are two special fields in there: The __internalNeo4jId__ and the __nodeLabels__. Both are critical when it comes to mapping the data to Java objects. The value of the __internalNeo4jId__ is either id(n) or the provided custom id but in the mapping process one known field to refer to has to exist. The __nodeLabels__ ensures that all defined labels on this node can be found and mapped. This is needed for situations when inheritance is used and you query not for the concrete classes or have relationships defined that only define a super-type.

Talking about relationships: If you have defined relationships in your entity, they will get added to the returned map as pattern comprehensions. The above return part will then look like:

RETURN n{.first_name, …​, Person_Has_Hobby: [(n)-[:Has]→(n_hobbies:Hobby)|n_hobbies{__internalNeo4jId__: id(n_hobbies), .name, nodeLabels: labels(n_hobbies)}]}

The map projection and pattern comprehension used by SDN ensures that only the properties and relationships you have defined are getting queried.

Migrating from SDN+OGM to SDN

Known issues with past SDN+OGM migrations

SDN+OGM has had quite a history over the years and we understand that migrating big application systems is neither fun nor something that provides immediate profit. The main issues we observed when migrating from older versions of Spring Data Neo4j to newer ones are roughly in order the following:

Having skipped more than one major upgrade

While Neo4j-OGM can be used stand-alone, Spring Data Neo4j cannot. It depends to large extend on the Spring Data and therefore, on the Spring Framework itself, which eventually affects large parts of your application. Depending on how the application has been structured, that is, how much the any of the framework part leaked into your business code, the more you have to adapt your application. It get’s worse when you have more than one Spring Data module in your application, if you accessed a relational database in the same service layer as your graph database. Updating two object mapping frameworks is not fun.

Relying on a embedded database configured through Spring Data itself

The embedded database in a SDN+OGM project is configured by Neo4j-OGM. Say you want to upgrade from Neo4j 3.0 to 3.5, you can’t without upgrading your whole application. Why is that? As you chose to embed a database into your application, you tied yourself into the modules that configure this embedded database. To have another, embedded database version, you have to upgrade the module that configured it, because the old one does not support the new database. As there is always a Spring Data version corresponding to Neo4j-OGM, you would have to upgrade that as well. Spring Data however depends on Spring Framework and than the arguments from the first bullet apply.

Being unsure about which building blocks to include

It’s not easy to get the terms right. We wrote the building blocks of an SDN+OGM setting here. It may be so that all of them have been added by coincidence and you’re dealing with a lof of conflicting dependencies.

Backed by those observations, we recommend to make sure you’re using only the Bolt or http transport in your current application before switching from SDN+OGM to SDN. Thus, your application and the access layer of your application is to large extend independent from the databases version. From that state, consider moving from SDN+OGM to SDN.

Prepare the migration from SDN+OGM Lovelace or SDN+OGM Moore to SDN

The Lovelace release train corresponds to SDN 5.1.x and OGM 3.1.x, while the Moore is SDN 5.2.x and OGM 3.2.x.

First, you must make sure that your application runs against Neo4j in server mode over the Bolt protocol, which means work in two of three cases:

You’re on embedded

You have added org.neo4j:neo4j-ogm-embedded-driver and org.neo4j:neo4j to you project and starting the database via OGM facilities. This is no longer supported and you have to setup a standard Neo4j server (both standalone and cluster are supported).

The above dependencies have to be removed.

Migrating from the embedded solution is probably the toughest migration, as you need to setup a server, too. It is however the one that gives you much value in itself: In the future, you will be able to upgrade the database itself without having to consider your application framework, and your data access framework as well.

You’re using the HTTP transport

You have added org.neo4j:neo4j-ogm-http-driver and configured an url like http://user:[email protected]:7474. The dependency has to be replaced with org.neo4j:neo4j-ogm-bolt-driver and you need to configure a Bolt url like bolt://localhost:7687 or use the new neo4j:// scheme, which takes care of routing, too.

You’re already using Bolt indirectly

A default SDN+OGM project uses org.neo4j:neo4j-ogm-bolt-driver and thus indirectly, the pure Java Driver. You can keep your existing URL.

Migrating

Once you have made sure, that your SDN+OGM application works over Bolt as expected, you can start migrating to SDN.

  • Remove all org.neo4j:neo4j-ogm-* dependencies

  • Configuring SDN through a org.neo4j.ogm.config.Configuration bean is not supported, instead of, all configuration of the driver goes through our new Java driver starter. You will especially have to adapt the properties for the url and authentication, see Listing 51

You cannot configure SDN through XML. In case you did this with your SDN+OGM application, make sure you learn about annotation-driven or functional configuration of Spring Applications. The easiest choice these days is Spring Boot. With our starter in place, all the necessary bits apart from the connection URL and the authentication is already configured for you.
Listing 51. Old and new properties compared
# Old
spring.data.neo4j.embedded.enabled=false # No longer support
spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=secret

# New
spring.neo4j.uri=bolt://localhost:7687
spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=secret
Those new properties might change in the future again when SDN and the driver will eventually replace the old setup fully.

And finally, add the new dependency, see Chapter 4 for both Gradle and Maven.

You’re than ready to replace annotations:

Old New

org.neo4j.ogm.annotation.NodeEntity

org.springframework.data.neo4j.core.schema.Node

org.neo4j.ogm.annotation.GeneratedValue

org.springframework.data.neo4j.core.schema.GeneratedValue

org.neo4j.ogm.annotation.Id

org.springframework.data.neo4j.core.schema.Id

org.neo4j.ogm.annotation.Property

org.springframework.data.neo4j.core.schema.Property

org.neo4j.ogm.annotation.Relationship

org.springframework.data.neo4j.core.schema.Relationship

org.springframework.data.neo4j.annotation.EnableBookmarkManagement

No replacement, not needed

org.springframework.data.neo4j.annotation.UseBookmark

No replacement, not needed

Several Neo4j-OGM annotations have not yet a corresponding annotation in SDN, some will never have. We will add to the list above as we support additional features.
Bookmarkmanagement

Both @EnableBookmarkManagement and @UseBookmark as well as the org.springframework.data.neo4j.bookmark.BookmarkManager interface and it’s only implementation org.springframework.data.neo4j.bookmark.CaffeineBookmarkManager are gone and are not needed anymore.

SDN uses Bookmarks for all transactions, without configuration. You can remove the bean declaration of CaffeineBookmarkManager as well as the the dependency to com.github.ben-manes.caffeine:caffeine.

Building Spring Data Neo4j

Requirements

  • JDK 8+ (Can be OpenJDK or Oracle JDK)

  • Maven 3.6.2 (We provide the Maven wrapper, see mvnw respectively mvnw.cmd in the project root; the wrapper downloads the appropriate Maven version automatically)

  • A Neo4j 3.5.+ database, either

About the JDK version

Choosing JDK 8 is a decision influenced by various aspects

  • SDN is a Spring Data project. Spring Data commons baseline is still JDK 8 and so is Spring Frameworks baseline. Thus it is only natural to keep the JDK 8 baseline.

  • While there is an increase of projects started with JDK 11 (which is Oracles current LTS release of Java), many existing projects are still on JDK 8. We don’t want to lose them as users right from the start.

Running the build

The following sections are alternatives and roughly sorted by increased effort.

All builds require a local copy of the project:

Listing 52. Clone SDN
$ git clone [email protected]:spring-projects/spring-data-neo4j.git

Before you proceed, verify your locally installed JDK version. The output should be similar:

Listing 53. Verify your JDK
$ java -version
java version "12.0.1" 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
With Docker installed
Using the default image

If you don’t have Docker installed, head over to Docker Desktop. In short, Docker is a tool that helps you running lightweight software images using OS-level virtualization in so called containers.

Our build uses Testcontainers Neo4j to bring up a database instance.

Listing 54. Build with default settings on Linux / macOS
$ ./mvnw clean verify

On a Windows machine, use

Listing 55. Build with default settings on Windows
$ mvnw.cmd clean verify

The output should be similar.

At the moment, this build tests against Neo4j 3.5, as 4.0 is not yet available on Docker Hub. As a consequence, tests requiring a reactive capable database, are skipped, as this is a feature of Neo4j 4.0.

Using another image

The image version to use can be configured through an environmental variable like this:

Listing 56. Build using a different Neo4j Docker image
$ SDN_NEO4J_VERSION=3.5.11-enterprise SDN_NEO4J_ACCEPT_COMMERCIAL_EDITION=yes ./mvnw clean verify

Here we are using 3.5.11 enterprise and also accept the license agreement.

Consult your operating system or shell manual on how to define environment variables if specifying them inline does not work for you.

Against a locally running database
Running against a locally running database will erase its complete content.

Building against a locally running database is faster, as it does not restart a container each time. We do this a lot during our development.

You can get a copy of Neo4j at our download center free of charge. Especially you can get the current prelease of Neo4j 4.0, supporting all the reactive features.

Please download the version applicable to your operating system and follow the instructions to start it. A required step is to open a browser and go to http://localhost:7474 after you started the database and change the default password from neo4j to something of your liking.

After that, you can run a complete build by specifying the local bolt URL:

Listing 57. Build using a locally running database
$ SDN_NEO4J_URL=bolt://localhost:7687 SDN_NEO4J_PASSWORD=secret ./mvnw clean verify

Summary of environment variables controlling the build

Name Default value Meaning

SDN_NEO4J_VERSION

3.5.6

Version of the Neo4j docker image to use, see Neo4j Docker Official Images

SDN_NEO4J_ACCEPT_COMMERCIAL_EDITION

no

Some tests may require the enterprise edition of Neo4j. We build and test against the enterprise edition internally, but we won’t force you to accept the license if you don’t want to.

SDN_NEO4J_URL

not set

Setting this environment allows connecting to a locally running Neo4j instance. We use this a lot during development.

SDN_NEO4J_PASSWORD

not set

Password for the neo4j user of the instance configured with SDN_NEO4J_URL.

You need to set both SDN_NEO4J_URL and SDN_NEO4J_PASSWORD to use a local instance.

Checkstyle and friends

There is no quality gate in place at the moment to ensure that the code/test ratio stays as is, but please consider adding tests to your contributions.

We have some rather mild checkstyle rules in place, enforcing more or less default Java formatting rules. Your build will break on formatting errors or something like unused imports.

jQAssistant

We also use jQAssistant, a Neo4j-based tool, to verify some aspects of our architecture. The rules are described with Cypher and your build will break when they are violated:

Coding Rules

The following rules are checked during a build:

API

Ensure that we publish our API in a sane and consistent way.

General considerations

We use @API Guardian to keep track of what we expose as public or internal API. To keep things both clear and concise, we restrict the usage of those annotations to interfaces, classes (incl. constructors) and annotations.

Listing 58. @API Guardian annotations must not be used on fields
MATCH (c:Java) - [:ANNOTATED_BY] -> (a) - [:OF_TYPE] -> (t:Type {fqn: 'org.apiguardian.api.API'}),
      (p) - [:DECLARES] -> (c)
WHERE c:Member AND NOT c:Constructor
RETURN p.fqn, c.name

Public interfaces, classes or annotations are either part of internal or public API and have a status.

Listing 59. Define which Java artifacts are part of internal or public API
MATCH (c:Java) - [:ANNOTATED_BY] -> (a) - [:OF_TYPE] -> (t:Type {fqn: 'org.apiguardian.api.API'}),
      (a) - [:HAS] -> ({name: 'status'}) - [:IS] -> (s)
WHERE ANY (label IN labels(c) WHERE label in ['Interface', 'Class', 'Annotation'])
WITH  c, trim(split(s.signature, ' ')[1]) AS status
WITH  c, status,
      CASE status
        WHEN 'INTERNAL' THEN 'Internal'
        ELSE 'Public'
      END AS type
MERGE (a:Api {type: type, status: status})
MERGE (c) - [:IS_PART_OF] -> (a)
RETURN c,a
Internal API

See ADR-003.

Listing 60. Non abstract, public classes that are only part of internal API must be final
MATCH (c:Class) - [:IS_PART_OF] -> (:Api {type: 'Internal'})
WHERE c.visibility = 'public'
  AND coalesce(c.abstract, false) = false
  AND NOT exists(c.final)
RETURN c.name
Naming things

The following naming conventions are used throughout the project:

Listing 61. All Java types must be located in packages that start with org.springframework.data.neo4j.
MATCH
  (project:Maven:Project)-[:CREATES]->(:Artifact)-[:CONTAINS]->(type:Type)
WHERE
  NOT type.fqn starts with 'org.springframework.data.neo4j'
RETURN
  project as Project, collect(type) as TypeWithWrongName
Structuring things

Most of the time, the package structure under org.springframework.data.neo4j should reflect the main building parts.

Listing 62. The mapping package must not depend on any other SDN packages than schema and convert
MATCH (a:Main:Artifact)
OPTIONAL MATCH (a) -[:CONTAINS]-> (s:Package) WHERE s.fqn in ['org.springframework.data.neo4j.core.schema', 'org.springframework.data.neo4j.core.convert']
WITH collect(s) as allowed, a
MATCH (a) -[:CONTAINS]-> (p1:Package) -[:DEPENDS_ON]-> (p2:Package) <-[:CONTAINS]- (a)
WHERE p1.fqn = 'org.springframework.data.neo4j.core.mapping'
  AND NOT (p2 in allowed OR (p1) -[:CONTAINS]-> (p2))
RETURN p1,p2
Listing 63. The public support packages must not depend directly on the mapping package
MATCH (a:Main:Artifact)
MATCH (a) -[:CONTAINS]-> (p1:Package)
WHERE p1.fqn in [
        'org.springframework.data.neo4j.core.convert',
        'org.springframework.data.neo4j.core.schema',
        'org.springframework.data.neo4j.core.support',
        'org.springframework.data.neo4j.core.transaction'
      ]
WITH p1, a
MATCH (p1) - [:CONTAINS] -> (t:Type)
MATCH (t) - [:DEPENDS_ON] -> (t2:Type) <- [:CONTAINS] - (p2:Package) <-[:CONTAINS]- (a)
WHERE t2.fqn <> 'org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty'
  AND p2.fqn = 'org.springframework.data.neo4j.core.mapping'
RETURN t
Accessing the jQAssistant database

jQAssistant uses Neo4j to store information about a project. To access the database, please build the project as described above. When the build finishes, execute the following command:

Listing 64. Start jQAssistant
$ ./mvnw -pl org.springframework.data.neo4j:spring-data-neo4j jqassistant:server

Access the standard Neo4j browser at http://localhost:7474 and a dedicated jQA-Dashboard at http://localhost:7474/jqassistant/dashboard/.

The scanning and analyzing can be triggered individually, without going through the full verify again:

Listing 65. Manually scan and analyze the main project
$ ./mvnw -pl org.springframework.data.neo4j:spring-data-neo4j jqassistant:[email protected]
$ ./mvnw -pl org.springframework.data.neo4j:spring-data-neo4j jqassistant:[email protected]

Appendix

Unresolved directive in index.adoc - include::../../../../../spring-data-commons/src/main/asciidoc/repository-query-keywords-reference.adoc[leveloffset=+1] Unresolved directive in index.adoc - include::../../../../../spring-data-commons/src/main/asciidoc/repository-query-return-types-reference.adoc[leveloffset=+1]