Neo4j - OGM Object Graph Mapper

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

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

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. However, starting with Spring Data Neo4j 6.x (Spring Boot 2.4), Neo4j-OGM is not needed anymore.

Please note that, intentionally, Neo4j-OGM 4+ does not work as a drop-in replacement for Spring Data 5.x.

How are labels generated when using inheritance?

All concrete classes generate a label, but 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.

Compatibility

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

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 Only supports connections via Bolt.

3 No longer actively developed.

4 No longer actively developed or supported.

5 Neo4j-OGM 3.2.24+ only.

6 Technically working, but not officially supported