How to use Quarkus and Neo4j OGM together. Neo4j OGM enables convenient integration and object-graph mapping (OGM) in your Quarkus application.
The example project is described in detail on the Neo4j Developer Site
The project uses Java 17 and Quarkus 2.14.2.
These are the components of our Web Application:
-
Application Type: Quarkus
-
Persistence Access: Quarkus-Neo4j-OGM
-
Database: Neo4j-Server 4.x
-
Frontend: jquery, bootstrap, d3.js
Provision a database quickly with Neo4j Sandbox or Neo4j Aura.
The project is built via Maven:
mvn package
Additionally, you can execute the integration test, which fires up a local Neo4j instance via Testcontainers:
mvn test-compile failsafe:integration-test failsafe:verify
You run the Quarkus application via executable JAR:
java -jar target/quarkus-app/quarkus-run.jar
Then, the application is available under http://localhost:8080/
Alternatively, you can use Quarkus' development mode:
mvn quarkus:dev
This enables you to edit the Java code while the application keeps running.
Per default, the example runs with a generally-available database under https://demo.neo4jlabs.com:7473
You can also run a local Neo4j database with the example project as Docker container. For this, execute the ./run-graph-db.sh
script, and change the Quarkus application properties under src/main/resources/application.properties
as follows:
quarkus.neo4j.uri=bolt://localhost:7687 quarkus.neo4j.authentication.username=neo4j quarkus.neo4j.authentication.password=test
If you want to run your Neo4j database in a different way, you can also execute the :guide movies
in your database console, and execute the long Cypher statement to fill your database with the movie data from the second slide of that guide.
Once the application is running, you can examine the UI under http://localhost:8080
The different scenarios of finding movies with certain queries and advanced example can be found under the “Scenarios” dropdown.
In order to comprehend how the queries are executed, you can follow the code in the MovieResource, SearchResource, ActorsResource, and GraphResource classes.
All JAX-RS resource classes access application-scoped beans, such as Searches, which access the Neo4j database via OGM Session
.
The OGM session is created via SessionFactory
which can be injected in the classes since we’re using the org.neo4j:neo4j-ogm-quarkus
dependency in our project:
@ApplicationScoped
public class Searches {
@Inject
SessionFactory sessionFactory;
public List<Movie> searchMoviesByTitle(String title) {
Session session = sessionFactory.openSession();
Iterable<Movie> iterable = session.query(Movie.class, "MATCH (movie:Movie) WHERE movie.title CONTAINS $title RETURN movie", Map.of("title", title));
// [...]
}
}
Another example is the mapping of arbitrary structures to DTO classes or Java records, as shown in Persons:
public List<ActorRecommendation> recommendCoActor(String name) {
Session session = sessionFactory.openSession();
return session.queryDto(" MATCH (actor:Person {name: $name})- [...] " +
" [...] " +
" RETURN cocoActors.name AS actor, count(*) AS strength ORDER BY strength DESC",
Map.of("name", name), ActorRecommendation.class);
}
The return type of queryDto
, here ActorRecommendation
can either be a simple class, that doesn’t need any annotations but does need a public, no-args constructor, or a Java record as it is the case in our example.