sebastiandaschner blog

Neo4J OGM with Quarkus (Video)

#neo4j #quarkus monday, may 18, 2020

In the following video I show an example Quarkus application that uses a Neo4J database and Neo4J OGM.

Have a look at the example project on GitHub.


I’ve created an example domain for coffee beans with certain flavor profiles that we can query and match. The red nodes are coffee beans that are from a certain country and taste like certain flavors. Bean origin countries are also “known for” certain flavors.

neo4j coffee beans

In the current version 1.4.2.Final, Quarkus comes with basic Neo4J support, but as of writing this not with included support for OGM mapping. However, we can add the support with one simple producer that exposes a Neo4J OGM SessionFactory:

import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.session.SessionFactory;

public class SessionFactoryProducer {

    public static final String PACKAGE = "";

    @ConfigProperty(name = "quarkus.neo4j.uri")
    String databaseUri;

    @ConfigProperty(name = "quarkus.neo4j.authentication.username")
    String username;

    @ConfigProperty(name = "quarkus.neo4j.authentication.password")
    String password;

    SessionFactory produceSessionFactory() {
        Configuration neoConfig = new Configuration.Builder()
                .credentials(username, password)

        return new SessionFactory(neoConfig, PACKAGE);

    void disposeSessionFactory(@Disposes SessionFactory sessionFactory) {

Now we can inject the SessionFactory in our beans and use it to query our graph database:

import org.neo4j.ogm.session.*;

public class CoffeeBeans {

    SessionFactory sessionFactory;

    public List<CoffeeBean> getCoffeeBeans() {
        Session session = sessionFactory.openSession();
        return new ArrayList<>(session.loadAll(CoffeeBean.class,
                new SortOrder("name"), 1));

    public List<CoffeeBean> getCoffeeBeansSpecificFlavor(String flavor) {
        Session session = sessionFactory.openSession();

        Iterable<CoffeeBean> result = session.query(CoffeeBean.class,
                "MATCH (b:CoffeeBean)-[:TASTES]->(:Flavor {description: $flavor})\n" +
                "MATCH (b)-[isFrom:IS_FROM]->(country)\n" +
                "MATCH (b)-[tastes:TASTES]->(flavor)\n" +
                "RETURN b, collect(isFrom), collect(country)," +
                " collect(tastes), collect(flavor)\n" +
                "ORDER by;",
            Map.of("flavor", flavor));

        return resultList(result);


My example graph looks as follows, with the CoffeeBean objects being mapped via Neo4J OGM:

import org.neo4j.ogm.annotation.*;

public class CoffeeBean {

    public String name;

    public Set<Origin> origins = new HashSet<>();

    public Roast roast;

    public Set<FlavorProfile> flavorProfiles = new HashSet<>();


Watch the video and have a look at the project on GitHub to get the full picture.

Have fun exploring your graph! As some further exercise, you can write queries that match coffee beans that have a certain flavor as their “main” flavor (the one with the highest percentage), or beans with similar flavor profiles, etc.


Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java: