Neo4j-OGM is a fast object-graph mapping library for Neo4j, optimised for server-based installations utilising Cypher.
It aims to simplify development with the Neo4j graph database and like JPA, it uses annotations on simple POJO domain objects.
Please have a look at the current supported versions and which combinations we recommend: Recommended versions
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-core</artifactId>
<version>4.0.13</version>
</dependency>
<dependency> <!-- If you're using the Bolt driver -->
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-bolt-driver</artifactId>
<version>4.0.13</version>
</dependency>
@NodeEntity
public class Actor {
@Id @GeneratedValue
private Long id;
private String name;
@Relationship(type = "ACTS_IN", direction = Relationship.OUTGOING)
private Set<Movie> movies = new HashSet<>();
public Actor() {
}
public Actor(String name) {
this.name = name;
}
public void actsIn(Movie movie) {
movies.add(movie);
movie.getActors().add(this);
}
}
@NodeEntity
public class Movie {
@Id @GeneratedValue
private Long id;
private String title;
private int released;
public Movie() {
}
public Movie(String title, int year) {
this.title = title;
this.released = year;
}
}
The either configure Neo4j-OGM with properties files, or programmatically.
Please see examples here.
//Set up the Session
SessionFactory sessionFactory = new SessionFactory(configuration, "movies.domain");
Session session = sessionFactory.openSession();
Movie movie = new Movie("The Matrix", 1999);
Actor keanu = new Actor("Keanu Reeves");
keanu.actsIn(movie);
Actor carrie = new Actor("Carrie-Ann Moss");
carrie.actsIn(movie);
//Persist the movie. This persists the actors as well.
session.save(movie);
//Load a movie
Movie matrix = session.load(Movie.class, movie.getId());
for(Actor actor : matrix.getActors()) {
System.out.println("Actor: " + actor.getName());
}
We do offer two offical integrations:
-
The Neo4j-OGM Quarkus Extension, which works nicely with Quarkus' "do everything and then some" at buildtime-approach
-
The Neo4j-OGM Spring Data fork, which provides continued support for Spring Data Neo4j 5, but for modern Spring versions
The reference guide is the best place to get started.
You can also post questions in our community forums or on StackOverflow.
To use the latest development version, just clone this repository and run mvn clean install
.
The tests default to Bolt.
If you want to change this, you have to define the property ogm.properties
when calling Maven.
e.g. ./mvnw clean verify -Dogm.properties=ogm-bolt.properties
.
For testing we are using TestContainers.
The default image right now is neo4j:5
.
If you want to use other images or the enterprise edition, you have to opt-in.
Here is a list of the possible environment variables you can provide.
Variable | Description | Default value |
---|---|---|
|
Use enterprise edition and accept the Neo4j licence agreement. |
|
|
Image to be used by TestContainers. |
|