Home » Java » Enterprise Java » Neo4J OGM with Quarkus

About Sebastian Daschner

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.

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.

0 0 vote
Article Rating
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x