This tutorial is based on:
Quarkus - Simplified Hibernate ORM WITH PANACHE
H2 Database Installation
For this tutorial you actually don’t need to download H2. But it’s
nice to have so you can inspect your database.
- Download the “Last Stable” version from H2 website
- Extract the file
- Go to h2 directory
- Run:
java -jar bin/h2-1.4.199.jar
(ps: replace with appropriate version)
- It will open the H2 Console on the browser
- Change
JDBC URL
to jdbc:h2:~/panache-h2
- Click in Connect
Let’s create our table
After you run the command above, disconnect and close H2 Console.
Creating the Maven project
Edit src/main/resources/application.properties
Let’s create our classes
It’s just two entities and one resource.
Status.java
Person.java
PanacheEntity.java already has the ID field and it extends PanacheEntityBase.java
PersonResource.java
Let’s use Swagger so we can test our APIs
- Add the following dependency to
pom.xml
:
- Add swagger-ui to
application.properties
- Call the GET method to see if they were created.
!!! Important: if you pass the ID field to persist:
… you’ll get the error:
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: org.acme.hibernate.orm.panache.Person
I think the error occurs because the id is set but no Person.find
was used,
so the entity is not attached. It’s just a guess … =)
Now let’s update our entity
- Add to
PersonResource.java
- Reload Swagger UI page and execute the PUT method:
- Now you have to pass the ID!
- Update Bob’s name to John
- Call the GET method again and check if the update worked.
That’s it!
And if you didn’t notice, you didn’t even need to restart Quarkus!
This is thanks to the amazing hot-reload capability that Quarkus provides!