Sunday, April 21, 2024
HomeJavaPouring Espresso into the Matrix: Constructing Java Functions on Neo4j - Java...

Pouring Espresso into the Matrix: Constructing Java Functions on Neo4j – Java Code Geeks

There all the time appears to be a set of content material round a single, particular expertise, nevertheless it’s tougher to seek out content material for utilizing a couple of expertise collectively. On this weblog put up, we’ll step by way of constructing an utility in Java that highlights the capabilities and powers of what a graph database may also help builders obtain.

Let’s code!

We’ll want a number of issues arrange earlier than we write our Java code. The very first thing is a Neo4j database. There are a number of choices to do that, however the best is to start out a free occasion of Neo4j AuraDB, which is a database-as-a-service. You simply want to join an account at and step by way of the prompts to create and begin an occasion (be aware: select the choice for an empty occasion on the information set step and you should definitely both obtain the credentials file or save the credentials someplace you’ll be able to reference them).

Subsequent, we have to load the information for Java variations into the database. To do that, as soon as your database occasion is up and working, click on the Question button on the suitable aspect of the panel and replica/paste the Cypher statements from the graph-demo-datasets Github repository.

As soon as the information has completed loading, we will confirm the information mannequin by working the next question within the question editor command line.

Data model - JavaVersion to VersionDiffs

Subsequent, we have to arrange our utility undertaking template. There are lots of methods to create a Java undertaking, however we’ll make the most of Spring Initializr to create a Spring Boot utility.

Spring Initializr - creating the project

The Spring Initializr is discovered at We’re going to decide on Maven for the undertaking kind right this moment, then Java for the language. We are able to depart the defaults for the remainder of the fields in that column.

In the suitable column, we’ll select what dependencies for the undertaking. Click on on the Add Dependencies button, then kind in and click on on every one. We’ll want the Neo4j and the Spring Net dependencies. Lastly, whereas not required, I’ve chosen to make use of Lombok for this undertaking, which cuts down on boilerplate by way of annotations.

Now we will generate the undertaking with the Generate button on the backside, which is able to obtain the undertaking to our machine as a zipper file. Then we will unzip it and open it in an IDE.

All of the code written for right this moment’s demo is on the market within the associated Github repository (branches step-one and step-two). The primary little bit of code we have to write are a number of properties to hook up with our Neo4j database. Let’s open the file and fill in some particulars.


Within the place of the non-meaningful values on the suitable aspect of the equals indicators, copy/paste within the credentials out of your particular Neo4j AuraDB occasion. These will be discovered within the setting file you downloaded if you created the occasion, with exception of the database. Until you could have made a aware effort to modify from the default database, you’ll be able to put neo4j as the worth for that property.

Now we will begin coding our information area into the appliance, beginning with the JavaVersion entity.

public class JavaVersion {
    personal remaining String javaVersion;
    personal String identify, codeName, standing, apiSpec;
    personal LocalDate gaDate, eolDate;

The primary line makes use of the Lombok annotation of @Information, which is able to generate getters, setters, equals, hashcode, and toString strategies, in addition to a constructor with required arguments for the category. The @Node annotation tells the appliance that this entity maps to a database entity. Since we’re working with Neo4j, and its area mannequin makes use of nodes and relationships for entities, we have to search for nodes within the database containing these particulars.

Throughout the class, we will specify a number of fields as properties. Our first property is the javaVersion, which is the distinctive id discipline for the entity. We are able to notate that with the @Id annotation, and in addition use the @Property annotation to map a mismatch in identify between the database and utility. The database accommodates the property named model for the Java model; nonetheless, model is a reserved key phrase in Java, so as a substitute, we used javaVersion within the utility.

The final two traces include further properties for identify, code identify, standing, API specification, common availability date, and end-of-life date.

Subsequent, we have to add a repository interface that enables us to name strategies to work together with information within the database.

public interface JavaVersionRepo extends Neo4jRepository<JavaVersion, String> {

This repository extends the Neo4jRepository<> interface and works with the JavaVersion area kind, which has an Id kind of String. We don’t have to outline any strategies as a result of we will make the most of Spring’s derived strategies it gives out-of-the-box. Implementations of sure strategies equivalent to findAll(), findById(), and so forth typically look the identical for a lot of builders, so Spring gives commonplace implementations for these. As all the time, builders can customise or write their very own strategies for particular circumstances and wishes.

Subsequent, let’s outline our controller class to arrange a REST endpoint for us to name and retrieve information from Neo4j.

public class JavaVersionController {
    personal remaining JavaVersionRepo javaVersionRepo;

    Iterable<JavaVersion> findAllJavaVersions() { return javaVersionRepo.findAll(); }

This class has three annotations – @RestController, @RequestMapping(), and @AllArgsConstructor. @RestController tells Spring this shall be our relaxation controller class, and the @RequestMapping() units up our endpoint (in our case, /variations) for us to name to be able to entry the strategies outlined on this class. The ultimate annotation of @AllArgsConstructor is a Lombok annotation that creates a constructor with all arguments. That will get used once we inject our repository interface within the first line contained in the controller class.

Inside the category definition, we inject our repository, after which outline a way for retrieving the Java variations from the database. The @GetMapping annotation is a shortcut annotation that specifies a request mapping for a GET technique. The following line defines that technique. We count on a number of Java model entities to return, so we arrange a return kind of Iterable<JavaVersion> and identify our technique findAllJavaVersions(). Inside the tactic, we return the outcomes of calling the Spring-derived findAll() technique from the repository interface (repo).

This creates a place to begin the place we will check retrieving Java variations from Neo4j. Let’s run the appliance! As soon as began through the IDE or command line, we will navigate to an internet browser with the URL localhost:8080/variations to see our information.

Test application - Java version entities

At this level, we haven’t taken benefit of the graph as a result of we aren’t utilizing relationships between entities (solely retrieving JavaVersion entities). Let’s head again to our code and repair that!

Including relationships

We are able to’t have a relationship and not using a node on the opposite finish, so we have to add our VersionDiff nodes from our database mannequin to our utility mannequin by creating one other information class.

public class VersionDiff {
    personal remaining Lengthy neoId;
    personal String fromVendor, fromVersion, toVendor, toVersion;

The annotations on this class are the identical as our JavaVersion class, so we will bounce contained in the curly braces. We’re utilizing the @Id annotation to mark this variable because the distinctive identifier for the category, and in addition utilizing the @GeneratedValue annotation as a result of this discipline is just not a user-provided worth, however a system-generated one. The sphere is the interior Neo4j id as a result of this class doesn’t have a single, clear enterprise key. The final line lists the remainder of the fields – which vendor and model we’re beginning with for the evaluate, and which vendor and model we’re ending with for the evaluate.

Now we have to join the JavaVersion and VersionDiff utility entities along with a relationship. To try this, we have to return to the JavaVersion area class.

public class JavaVersion {
    <entity fields>

    personal Record<VersionDiff> olderVersionDiffs;

    personal Record<VersionDiff> newerVersionDiffs;

There are two @Relationship annotations added. The primary one is evaluating a JavaVersion with any earlier variations launched (i.e. Java 11 in comparison with 9, 8, 6, and so forth). We use the @Relationship annotation to specify this discipline maps crossing over a relationship entity within the database to be able to pull the VersionDiff entities on the opposite aspect. The second @Relationship annotation is on the lookout for model diffs which might be newer than the model we’re beginning with (i.e. Java 11 in comparison with 14, 17, 19, and so forth).

Mapping each of those relationships matches our information mannequin picture we noticed earlier, the place there are two sorts of relationships between JavaVersion entities and VersionDiff entities for evaluating older and newer model releases of the language.

Let’s check our modifications! Restart the appliance and navigate/refresh our browser to localhost:8080/variations.

Test application - Java versions and diffs

We are able to see the associated entities returning, though our information set doesn’t embrace diffs for Java model 1.0, in order that’s why we see empty lists within the Java 1.0 outcomes and why we don’t see older variations within the Java 1.1 outcomes.

Wrap Up!

In right this moment’s put up, we noticed how graphs and Java can work collectively by constructing a Java utility that linked to a Neo4j graph database. We noticed the capabilities of the graph and the way the information mannequin between database and utility are refreshingly comparable.

There are such a lot of extra issues we’ll discover sooner or later, equivalent to further options for creating customized queries and area class projections. Till then, glad coding!




Please enter your comment!
Please enter your name here

Most Popular

Recent Comments