Tips to use Hibernate ORM with Quarkus profiles and live coding mode
Quarkus live coding (aka dev mode) is really useful for applications that mix front end or services and database access. There are a few common approaches to make the best of it.
The first choice is to use
quarkus.hibernate-orm.database.generation=drop-and-create in conjunction with
That way for every change to your app and in particular to your entities, the database schema will be properly recreated
and your data fixture (stored in
import.sql) will be used to repopulate it from scratch.
This is best to perfectly control your environment and works magic with Quarkus live coding mode:
your entity changes or any change to your
import.sql is immediately picked up and the schema updated without restarting the application!
By default, Hibernate ORM, upon boot, will read and execute the SQL statements in the
The second approach is to use
This approach is best when you do many entity changes but
still need to work on a copy of the production data
or if you want to reproduce a bug that is based on specific database entries.
update is a best effort from Hibernate ORM and will fail in specific situations
including altering your database structure which could lead to data loss.
For example if you change structures related to the foreign key, Hibernate ORM might have to bail out.
But for development, these limitations are acceptable.
The third approach is to use
This approach is best when you are working on a copy of the production data but want to fully control the schema evolution.
Or if you use a database schema migration tool like Flyway.
When you make a change to an entity, make sure to adapt the database schema accordingly.
Do not use
It becomes really powerful when combined with Quarkus configuration profiles. You can define different configuration profiles to select different behaviors depending on your environment. This is great because you can define different combinations of Hibernate ORM properties matching the development style you currently need.
# By default, use the clean (data) slate approach %dev.quarkus.hibernate-orm.database.generation = drop-and-create %dev.quarkus.hibernate-orm.sql-load-script = import-dev.sql # Use this to incrementally work in your app while keeping data # Useful for rapid dev-check cycles with Quarkus dev mode %dev-with-data.quarkus.hibernate-orm.database.generation = update %dev-with-data.quarkus.hibernate-orm.sql-load-script = # Let's make sure we don't wipe the production data by accident! %prod.quarkus.hibernate-orm.database.generation = none %prod.quarkus.hibernate-orm.sql-load-script = no-file
# Rapid prototyping time! mvn compile quarkus:dev -Dquarkus.profile=dev-with-data # And now, let's be ready for prod mvn clean package -Pnative -Dquarkus.profile=prod
I hope you have found this tip useful. Hibernate ORM schema update, data fixture, Quarkus dev mode and its configuration profiles are a powerful combination!