Neo4j - OGM Object Graph Mapper

Goals
For Java developers that require a simple mechanism to manage their domain objects with Neo4j, this guide introduces the Neo4j Object Graph Mapping (OGM) library. This is the same library that powers Spring Data Neo4j but can also be used independently of Spring as well.
Prerequisites
You should be familiar with graph database concepts and the property graph model. You should have created an Neo4j AuraDB cloud instance, or installed Neo4j locally When developing with Neo4j, please use JDK 8 and your favorite IDE.

Intermediate

Neo4j-OGM

While using the Cypher query language alongside the Java driver works well for small projects and more advanced queries, the boilerplate of CRUD operations often gets tedious.

That’s where object graph mappers come in; they take your existing domain objects (POJOs) and map them to Neo4j. The great benefit with this approach is that with graph databases, there is no data-model impedence mismatch like there is with relational databases.

Besides the Neo4j-OGM, there is also Hibernate-OGM and others. If you want more abstraction Spring Data Neo4j can give you a Domain Driver Design (DDD) approach.

We created the Neo4j-OGM as a standalone, simple object graph mapper for Java which also acts as the base for Spring Data Neo4j.

The Neo4j-OGM uses Cypher in a consistent manner under the hood and can communicate via several transport protocols - bolt, http and embedded.

Features

The Neo4j-OGM supports the features you would expect:

  • Object graph mapping of annotated node- and relationship-entities

  • Neo4jSession for direct interaction with Neo4j

  • Fast class metadata scanning

  • Optimized management of data loading and change tracking for minimal data transfers

  • Multiple transports: binary (bolt), HTTP and embedded

  • Persistence lifecycle events

  • Query result projection to DTOs

Minimal Code Snippet

This code example is taken from the Example Project (see below).

@NodeEntity(label="Film")
public class Movie {

   @GraphId Long id;

   @Property(name="title")
   private String name;
}

@NodeEntity
public class Actor extends DomainObject {

   @Id @GeneratedValue
   private Long id;

   @Property(name="name")
   private String fullName;

   @Relationship(type="ACTED_IN", direction=Relationship.OUTGOING)
   private List<Role> filmography;

}

@RelationshipEntity(type="ACTED_IN")
public class Role {
    @Id @GeneratedValue  private Long relationshipId;
    @Property            private String title;
    @StartNode           private Actor actor;
    @EndNode             private Movie movie;
}

The Example Project

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

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

FAQ

Frequently Asked Questions (FAQ)

  1. What is the difference between Neo4j-OGM and Spring Data Neo4j (SDN)?

    Spring Data Neo4j (SDN) uses Neo4j-OGM under the covers. It’s like Spring Data JPA, where JPA/Hibernate is the underlay. Most of the power of SDN actually comes from Neo4j-OGM.

  2. How are labels generated when using inheritance?

    All concrete classes generate a label, abstract classes and interfaces not. If any kind of class or interface gets annotated with @NodeEntity or @NodeEntity(label="customLabel") it will generate a label. Any class annotated with @Transient will not generate a label.

Resources

Authors

The Neo4j and GraphAware Teams

Package

http://maven.org

Source

https://github.com/neo4j/neo4j-ogm

Docs

https://neo4j.com/docs/ogm-manual/current/

Article

http://neo4j.com/blog/neo4j-java-object-graph-mapper-released/

Example-University

https://github.com/neo4j-examples/neo4j-ogm-university

Versions

Consult the version table to determine which version of Neo4j-OGM to use with a particular version of Neo4j and related technologies.

Compatibility

Neo4j-OGM Version Neo4j Version1

3.2.x

3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x2, 4.1.x2, 4.2.x2, 4.3.x2,5, 4.4.x2,5

3.1.x

3.1.x, 3.2.x, 3.3.x, 3.4.x

3.0.x3

3.1.9, 3.2.12, 3.3.4, 3.4.4

2.1.x4

2.3.9, 3.0.11, 3.1.6

2.0.24

2.3.8, 3.0.7

2.0.14

2.2.x, 2.3.x

1 The latest supported bugfix versions.

2 These versions are only supported over Bolt.

3 These versions are no longer actively developed.

4 These versions are no longer actively developed or supported.

5 Neo4j-OGM 3.2.24+ only.

Transitive dependencies

The following table list transitive dependencies between specific versions of projects related to Neo4j-OGM. When reporting issues or asking for help on StackOverflow or neo4j-users slack channel always verify versions used (e.g through mvn dependency:tree) and report them as well.

Spring Boot Version Spring Data Release Train Spring Data Neo4j Version Neo4j-OGM Version

2.3

Neumann

5.3

3.2

2.2

Moore

5.2

3.2

2.1

Lovelace

5.1

3.1

2.01

Kay

5.0

3.0

1.52

Ingalls

4.2

2.1

1.42

Hopper

4.1

2.0

1 These versions are no longer actively developed.

2 These versions are no longer actively developed or supported.

Starting with Spring Boot 2.4, Spring Data Neo4j (version 6+) does not include Neo4j-OGM anymore.

Dependency Management

For building an application, your build automation tool needs to be configured to include the Neo4j-OGM dependencies.

Neo4j-OGM dependencies consist of neo4j-ogm-core, together with the relevant dependency declarations on the driver you want to use. Neo4j-OGM provides support for connecting to Neo4j by configuring one of the following Drivers:

  • neo4j-ogm-bolt-driver - Uses native Bolt protocol to communicate between Neo4j-OGM and a remote Neo4j instance.

  • neo4j-ogm-bolt-native-types - Support for all of Neo4j’s property types through the Bolt protocol.

  • neo4j-ogm-http-driver - Uses HTTP to communicate between Neo4j-OGM and a remote Neo4j instance.

  • neo4j-ogm-embedded-driver - Creates an in-memory Neo4j instance and uses it.

  • neo4j-ogm-embedded-native-types - Support for all of Neo4j’s property types in an embedded scenario.

Neo4j-OGM projects can be built using Maven, Gradle or any other build system that utilises Maven’s artifact repository structure.

Maven

In the <dependencies> section of your pom.xml add the following:

Maven dependencies
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-ogm-core</artifactId>
    <version>3.2.23</version>
    <scope>compile</scope>
</dependency>

<!-- Only add if you're using the Bolt driver -->
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-ogm-bolt-driver</artifactId>
    <version>3.2.23</version>
    <scope>runtime</scope>
</dependency>

<!-- Only add if you're using the HTTP driver -->
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-ogm-http-driver</artifactId>
    <version>3.2.23</version>
    <scope>runtime</scope>
</dependency>

<!-- Only add if you're using the Embedded driver -->
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-ogm-embedded-driver</artifactId>
    <version>3.2.23</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j</artifactId>
    <version>4.2.7</version>
    <scope>runtime</scope>
</dependency>

Please also have a look at the native type system to take advantage of Neo4j-OGM’s support for native temporal and spatial types.

Gradle

Ensure the following dependencies are added to you build.gradle:

Gradle dependencies
dependencies {
    compile 'org.neo4j:neo4j-ogm-core:3.2.23'
    runtime 'org.neo4j:neo4j-ogm-bolt-driver:3.2.23' // Only add if you're using the Bolt driver
    runtime 'org.neo4j:neo4j-ogm-http-driver:3.2.23' // Only add if you're using the HTTP driver
    runtime 'org.neo4j:neo4j-ogm-embedded-driver:3.2.23' //  Only add if you're using the Embedded driver
    runtime 'org.neo4j:neo4j:4.2.7' //  Only add if you're using the Embedded driver
}