Quarkus is a supersonic Java framework built with a cloud-native first mentality. It’s blazing fast in startup and has a much smaller memory footprint compared to Spring Boot. Quarkus is well integrated with Hibernate and works flawlessly. To simplify the Hibernate complexity, one can use Panache to achieve spring-data-repository-like results. In this article, I go through how to use MySQL in Quarkus with Hibernate and Panache.
In the last article, I covered how to build REST APIs using Quarkus. We’ve built a very simple Users
service that has CRUD functionality. For that example, we used SortedSet
as the dummy memory to hold a list of users. Now, it’s time to replace that with the actual database. For that, we will incorporate Hibernate and Panache to build repositories similar to Spring Data.
If you have not looked at Building REST APIs with Quarkus article, I highly recommend you to read that article first before proceeding further.
Vanilla Hibernate is difficult to use
We could use Hibernate solely. But that is overly complicated for such a simple example. And it is not something that most developers are accustomed for. Hence, I figured out having an abstraction layer, similar to Spring Data, is much better suited. That’s why I picked Panache.
Panache vs Spring Data
Quarkus has many useful extensions for various purposes. They are like different Spring frameworks or libraries. There are also many options available to work with databases. The top candidates are:
- Quarkus port of Spring Data JPA
- Panache
I aspired to try something new and opted for Panache. Though, there’s a major difference between Panache and Spring Data. Panache supports both Active Record and Repository Pattern. Thus, it gives more leeway to devs based on their needs.
In this article, we don’t use Active Records however. We stick to the conventional Repository Pattern since it’s a superior pattern and less messy to work with. If you are curious why, read My thoughts on Active record pattern article.
Wiring up Quarkus with Hibernate and Panache
We need to wire a few things up with Hibernate and Panache, to use MySQL in Quarkus.
Let’s start by adding the related dependencies. As we already added Hibernate
and MySQL
driver libraries, we just need to add Panache
dependency.
For completeness’ sake I have brought Hibernate
and MySQL
dependencies to the above snippet.
Adding application.properties
We should set MySQL details (such as credentials, port and so on) in application.properties
file.
That does not differ much from any Spring Boot configuration.
Changing the User
entity
Since, we already have the User entity, we just need to modify it. Let’s add some annotations. So we can use it with Hibernate.
Creating Panache repository
The next step is to create the UserRepository
. For that, we extendPanacheRepository
.
Adding @ApplicationScoped
allows Quarkus to manage the bean. This is a Javax annotation.
Surprisingly the class has no method. That’s because PanacheRepository
has some base methods that are sufficient for most CRUD operations.
Implementing User service
Implementing UserService
is straightforward. The only remotely challenging part could be how to inject the UserRepository
to the service.
As you can see we used @Inject
annotation to inject UserRepository
. The annotation added to the service constructor. Keep in mind that @Inject
is a Javax annotation, don’t mistake it with Google Inject annotation.
Updating UserController
The last step is to update the UserController
and get rid of the dummy set. And replace it with the actual implementation as follows,
Run the project
Let’s run the project,
To debug the project, run this command,
That opens port 5005
which then you can connect to it from your IDE.
Complete example
The complete implementation is available on my GitHub at the link below,
https://github.com/kasramp/quarkus-rest-example
Read the readme file for further instruction.
Inline/featured images credits
- Quarkus logo by Quarkus
- MySQL logo on Wikipedia
- Hibernate logo on Wikipedia
- Panache text by Alexandre Magois on Wikipedia (CC BY-SA 4.0)