Neo4j - OGM Object Graph Mapper

Goals
For Java developers that require a mechanism to manage their domain objects with Neo4j, this guide introduces the Neo4j Object Graph Mapping (OGM) library.
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

Intermediate

Neo4j-OGM

Developing Java Business Applications often requires mapping rich domain models to your database. The Neo4j-OGM library is a pure Java library that can persist (annotated) domain objects using Neo4j. It uses Cypher statements to handle those operations in Neo4j.

Neo4j-OGM supports tracking changes to minimize necessary updates and transitive persistence (reading and updating neighborhoods of an object).

The connection to Neo4j handled by a transport layer is based on the Bolt protocol and uses the Neo4j Java Driver.

Features

The Neo4j-OGM supports the features you would expect:

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

  • Fast class metadata scanning

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

  • Persistence lifecycle events

  • Query result projection to DTOs

Minimal Code Snippet

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

@NodeEntity
public class Movie {

   @Id @GeneratedValue
   Long id;

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

@NodeEntity
public class Actor {

   @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) up to version 5.3.x 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. Starting with Spring Data Neo4j 6.x (Spring Boot 2.4) does not need Neo4j-OGM anymore. Please note that, intentionally, Neo4j-OGM 4+ does not work as a drop-in replacement for Spring Data 5.x.

  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/

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

4.0.x2

4.4.x6, 5.x

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.x3

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 only support connections via 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.

6 Technical working but not officially supported

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 4.x provides only support for the Bolt driver, but for compatibility reasons you have to declare the dependency:

  • 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 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>4.0.0</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-ogm-bolt-driver</artifactId>
    <version>4.0.0</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:4.0.0'
    runtime 'org.neo4j:neo4j-ogm-bolt-driver:4.0.0'
}