Getting started with JHipster and Neo4j

Davide Fantuzzi
LARUS
Published in
5 min readMay 21, 2020

--

This is my first article and frankly I have no idea how to start. Same thing happened at my new job when they told me: “You have to do a Java application that works with Neo4j”.

Although did not used those exact same words, I wish I could have had this article on Medium to be inspired from.

Since my company is official Neo4j partner, I wasn’t really worried about that part of the project. On the other hand, I never really wrote an entire application in Java (I was a Java newbie at the time), and I was a bit scared.

A colleague suggested to take a look at JHipster. So…

TL;DR: this is a brief guide to get started with JHipster and Neo4j, we will go through the setup of the application.
This is not a comprehensive guide to JHipster, you can find some here.
This is not a Neo4j tutorial, you can find one here.

Cat tax
Photo by Raoul Droog on Unsplash

What is jHipster?

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.

There you go, exactly what I needed. JHipster come as a set of useful generators to help you to scaffold your Spring Boot application with a whole variety of different features and configurations.

You can choose between various persistence layer, and when we started using it, the Neo4j generator was in its early Alpha version (at the time of this writing, the generator is in Beta).

Let’s get this started

I could assume you already installed everything you need, as stated in the documentation, but let’s see what we need very quickly:

  • We obviously need Java, version 11. You can use SDK Man to easily handle your Java versions (and much more!)
  • We need NodeJS, since the JHipster generator is a NPM package. Here some links for you: NodeJS Download, Install via Package Manager. I suggest also to install NVM to manage your node versions.
  • Oh and we apparently need JHipster as well ¯\_(ツ)_/¯ and we install it via NPM: npm install -g generator-jhipster

And now…

Let’s go ahead and create a new MyAwesomeApp directory from our terminal and run the jhipster command from there.

$ mkdir MyAwesomeApp && cd MyAwesomeApp
$ jhipster

And the scaffolding wizard will start! My configuration looks like this:

  • Application Type: Monolithic (because we don’t need microservices)
  • Base Name: MyAwesomeApp
  • Java Package Name: org.myawesomeapp
  • Database: Neo4j
  • Build tool: Maven
  • Frontend Framework: React
  • i18n: no

For the other options I choose the default value.

Let JHipster take care of installing everything we need, and then we can now run our app.

Explore your first JHipster application

You might not have a version of Neo4j installed. That’s why JHipster comes with a set of docker-compose files set up for you. So, before running our backend, let’s switch on our Neo4j service.

$ docker-compose -f src/main/docker/neo4j.yml up -d
Creating docker_myawesomeapp-neo4j_1 ... done

You can now visit http://localhost:7474 to access Neo4j Browser. Leave username and password empty to access.

Let’s go back to our terminal and bring our app to life!

$ ./mvnw
...
----------------------------------------------------------
Application 'MyAwesomeApp' is running! Access URLs:
Local: http://localhost:8080/
External: http://192.168.1.123:8080/
Profile(s): [dev, swagger]
----------------------------------------------------------

Here we go! What did just happened? Maven just took care of compiling Javascript assets and building the Spring Boot application for us. We also already have 4 user set up in our DB.

Open the side bar by clicking the top left DB icon, then click jhi_user Node Label

So we can now browse to http://localhost:8080/ to see our Awesome App!

On the home page you can also find all the info you need to get started writing code with JHipster!

As you can read in the home page, we can both sign in as admin or simple user. We will sign in as admin. Username and password are admin.

I’ll let you play around a bit. Come back when you want to start working again!

So now we can start developing our app, let’s generate our first entity.

I will create an Hipster entity, with a required string field name and a required integer field age that can’t be greater than 60 (for whatever reason).

$ jhipster entity Hipster
...
================= Hipster =================
Fields
name (String) required
age (Integer) required max='60'

Then the generator will ask us some questions about the Services and the Resources. Feel free to answer whatever you like, just don’t make the entity read-only. These are my answers:

? Do you want to use separate service class for your business logic? Yes, generate a separate service class
? Do you want to use a Data Transfer Object (DTO)? No, use the entity directly
? Is this entity read-only? No
? Do you want pagination on your entity? Yes, with pagination links

Now it will generate all our files. There will be conflicts because JHipster will try to add lines of codes to existing files. At this time, we can overwrite everything (just press a when asked what to do with the conflicts), since we did not write anything on our own yet.

To see our new Entity on the App, we need to restart Spring Boot. Just stop the ./mvnw process and run it again.

Now you should see Hipster in the entity menu; feel free to insert a bunch of hipsters!

Every entity added to the system will appear under the Entities menu

We can see the created entities on Neo4j Browser:

Want to see what files has been generated? Just type git status and take a look at the status of your branch (yep, JHipster automatically inits a git repository for you).

Wanna dive deeper? Here’s an article by Michael Simons on the topic.

Final thoughts

At the end of the day, for our use case, JHipster saved us a lot of time. Obviously it’s not a developer and we can’t pretend it to write code that suits 100% our needs. You’ll have to tweak it a bit, or adapt the generated frontend to match your styles. For example, we used Material UI as component library, so we have to adjust the frontend code every time. But we are now live with our project eFraudy and works like a charm. Backend is stable and has everything you need to start building your app without worrying too much about security, deploy, builds, and so on.

Pretty handy the Docker integration, that creates a ready-to-deploy Docker image for you.

For any problem feel free to leave a comment. Or you can use StackOverflow (use the jhipster tag) for support, or Github for requesting features or addressing issues.

--

--

Davide Fantuzzi
LARUS

Backend Developer @ Switcho. Big fan of music and oxygen, for different reasons but both help me live.