Helidon MP with JPA (with Hibernate) — A step-by-step tutorial

Now that you are here , I assume you would be knowing that Helidon is a micro-service framework released by Oracle . As of now the framework supports two programming models namely Helidon MP and Helidon SE .

Comparison between helidon SE and helidon MP

In this tutorial we are primarily going to look at how to create a Helidon MP microservice with JPA support .

Helidon MP is an implementation of Microprofile — Java EE’s answer to microservices platform— which is primarily contributed by Eclipse and hence mostly known as Eclipse Microprofile .

Read more about Helidon here https://helidon.io/#/

Let’s look at how to create a project so that we can jump into this new interesting framework .

Step 1 — Create a quickstart Helidon application with maven archetype

Use a maven archetype to create the project skeleton. Helidon has released a set of archetypes to make life easy for us . Look at the available quickstart maven archetypes from Helidon here — https://mvnrepository.com/artifact/io.helidon.archetypes

In our case we are going to set up a project with the archetype helidon-quickstart-mp

The archetype will generate a quickstart helidon mp project with the necessary code .

The generated code will have a resource named GreetResource as a quickstart API .

Build the project and run the application as a jar .

Once application is started you can access the Greet resource like given below

And we will see the below JSON response

Step 2 — Add dependencies in pom.xml for JPA support

Add the CDI ( context and dependency injection ) dependencies for JPA .

Helidon provides out-of-the-box support for JPA implementations like EclipseLink and Hibernate . Most of the examples that are available around the internet are using EclipseLink , so in this tutorial I am going to go with Hibernate .

Let’s add the dependency for the helidon Hibernate support .

Next add the dependency for the database drivers . Here we are going to use PostgreSQL , since we will be running a postgresql database using Docker .

And finally we add the jakarta JPA and JTA libraries to support the CDI libraries we added earlier

That’s all with the pom.xml !

Step -3 Let’s set up a PostgreSQL database ( you can skip this step if you already have a database up and running )

We are going to set up a docker-compose file for starting up a docker container of PostgreSQL .

Create the docker-compose.yml file in the project root and define the details as given below

As you can see we have included an init.sql file which has the database script to create a database objects and to initialise the data.

contents of init.sql is as given below

To start the data base use the docker compose up command

Step 4 : Configuring Helidon to connect to the Database

Create persistence.xml configured for postgreSQL under the folder src/resources/META-INF

Now we need to configure the Datasource in the application.yaml so that a default datasource is injected to the EntityManager .

Content of the application.yaml is given below

Step -5 : Implementing an end-to-end JAX-RS based CRUD service

Create the following classes





Step-6 Add the entity in persistence-unit

Add the Airport entry in persistence.xml under the persistence-unit

Step-7 The final step . Build , Run and Test your project

Build the project with maven clean install

And now you can run the application

Test the application for the GET,POST and DELETE functions

To save a new entry

To save a delete an entry

So we saw how to configure a helidon MP microservice with JPA support and build and end-to-end CRUD service.

As you can see HelidonMP is not yet a full fledged framework like Springboot and we had to spend time on the initial configuration which gets done in spring-boot initializer out of the box. May be down the line the support will be added in Helidon .

You can download the code from my github repository .https://github.com/hareeshkg/jpa-helidon-sample

Techie , Travelista ,Philomath

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store