Enterprise Java

Neo4J OGM with Quarkus

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.

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:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.session.SessionFactory;
...
 
@ApplicationScoped
public class SessionFactoryProducer {
 
    public static final String PACKAGE = "com.sebastian_daschner.coffee.entity";
 
    @ConfigProperty(name = "quarkus.neo4j.uri")
    String databaseUri;
 
    @ConfigProperty(name = "quarkus.neo4j.authentication.username")
    String username;
 
    @ConfigProperty(name = "quarkus.neo4j.authentication.password")
    String password;
 
    @Produces
    SessionFactory produceSessionFactory() {
        Configuration neoConfig = new Configuration.Builder()
                .uri(databaseUri)
                .credentials(username, password)
                .useNativeTypes()
                .build();
 
        return new SessionFactory(neoConfig, PACKAGE);
    }
 
    void disposeSessionFactory(@Disposes SessionFactory sessionFactory) {
        sessionFactory.close();
    }
}

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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import org.neo4j.ogm.session.*;
...
 
@ApplicationScoped
public class CoffeeBeans {
 
    @Inject
    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 b.name;",
            Map.of("flavor", flavor));
 
        return resultList(result);
    }
 
    ...
}

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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
import org.neo4j.ogm.annotation.*;
...
 
@NodeEntity
public class CoffeeBean {
 
    @Id
    public String name;
 
    @Relationship("IS_FROM")
    public Set<Origin> origins = new HashSet<>();
 
    @Property
    public Roast roast;
 
    @Relationship("TASTES")
    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.

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Neo4J OGM with Quarkus (Video)

Opinions expressed by Java Code Geeks contributors are their own.

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button