Quarkus - Using Hibernate ORM and JPA

Hibernate ORM is the de facto JPA implementation and offers you the full breath of an Object Relational Mapper.It works beautifully in Quarkus.

More often than not, you need one persistence unit with few configuration options.In Quarkus, you just need to:

  • add your settings in application.properties

  • annotate your entities with @Entity and friends

and we make some opinionated choices and educated guesses.

In your pom.xml, add the following dependencies:

  • the Hibernate ORM extension

  • your JDBC driver extension (quarkus-jdbc-postgresql, quarkus-jdbc-h2, quarkus-jdbc-mariadb, …​)

  1. <dependencies>
  2. <!-- Hibernate ORM specific dependencies -->
  3. <dependency>
  4. <groupId>io.quarkus</groupId>
  5. <artifactId>quarkus-hibernate-orm</artifactId>
  6. </dependency>
  7. <!-- JDBC driver dependencies -->
  8. <dependency>
  9. <groupId>io.quarkus</groupId>
  10. <artifactId>quarkus-jdbc-postgresql</artifactId>
  11. </dependency>
  12. </dependencies>

Annotate your persistent objects with @Entity,then add the relevant configuration properties in application.properties.

  1. # configure your datasource
  2. quarkus.datasource.url = jdbc:postgresql://localhost:5432/hibernate_db
  3. quarkus.datasource.driver = org.postgresql.Driver
  4. quarkus.datasource.username = hibernate
  5. quarkus.datasource.password = hibernate
  6. # drop and create the database at startup (use `update` to only update the schema)
  7. quarkus.hibernate-orm.database.generation=drop-and-create

Note that these configuration properties are not the same ones as in your typical Hibernate ORM configuration file: they might differ in names, casing and don’t necessarily map 1:1 to each other.Please see below for the list of properties you can define.

An EntityManagerFactory will be created based on Quarkus datasource configuration as long as the Hibernate ORM extension is declared in your pom.xml.The dialect will be selected based on the JDBC driver.

You can then happily inject your EntityManager:

  1. @ApplicationScoped
  2. public class SantaClausService {
  3. @Inject
  4. EntityManager em; (1)
  5. @Transactional (2)
  6. public void createGift(String giftDescription) {
  7. Gift gift = new Gift();
  8. gift.setName(giftDescription);
  9. em.persist(gift);
  10. }
  11. }
  12. //and of course our entity
  13. @Entity
  14. public class Gift {
  15. private Long id;
  16. private String name;
  17. @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="giftSeq")
  18. public Long getId() {
  19. return id;
  20. }
  21. public void setId(Long id) {
  22. this.id = id;
  23. }
  24. public String getName() {
  25. return name;
  26. }
  27. public void setName(String name) {
  28. this.name = name;
  29. }
  30. }
1Inject your entity manager and have fun
2Mark your CDI bean method as @Transactional and the EntityManager will enlist and flush at commit.

To load some SQL statements when Hibernate ORM starts, add a import.sql in the root of your resources directory.It contains SQL DML statements terminated by a semicolon.This is useful to have a data set ready for your tests or demos.

Make sure to wrap methods modifying your database (e.g. entity.persist()) within a transaction. Marking aCDI bean method @Transactional will do that for you and make that method a transaction boundary. We recommend doingso at your application entry point boundaries like your REST endpoint controllers.

Properties to refine your Hibernate ORM configuration

There are optional properties useful to refine your EntityManagerFactory or guide guesses of Quarkus.

Configuration property fixed at build time - ️ Configuration property overridable at runtime

Configuration propertyTypeDefault
quarkus.hibernate-orm.dialectClass name of the Hibernate ORM dialect. The complete list of bundled dialects is available in theHibernate ORM JavaDoc.
Not all the dialects are supported in GraalVM native executables: we currently provide driver extensions for PostgreSQL,MariaDB, Microsoft SQL Server and H2.

string

quarkus.hibernate-orm.dialect.storage-engine

The storage engine to use when the dialect supports multiple storage engines.

E.g. MyISAM or InnoDB for MySQL.

string

quarkus.hibernate-orm.sql-load-script

Name of the file containing the SQL statements to execute when Hibernate ORM starts.Its default value differs depending on the Quarkus launch mode:

  • In dev and test modes, it defaults to import.sql.Simply add an import.sql file in the root of your resources directoryand it will be picked up without having to set this property.Pass no-file to force Hibernate ORM to ignore the SQL import file.

  • In production mode, it defaults to no-file.It means Hibernate ORM won’t try to execute any SQL import file by default.Pass an explicit value to force Hibernate ORM to execute the SQL import file.

If you need different SQL statements between dev mode, test (@QuarkusTest) and in production, use Quarkusconfiguration profiles facility.

application.properties

  1. %dev.quarkus.hibernate-orm.sql-load-script = import-dev.sql
  2. %test.quarkus.hibernate-orm.sql-load-script = import-test.sql
  3. %prod.quarkus.hibernate-orm.sql-load-script = no-file
Quarkus supports .sql file with SQL statements or comments spread over multiple lines.Each SQL statement must be terminated by a semicolon.

stringimport.sql (DEV,TEST)

quarkus.hibernate-orm.batch-fetch-size

The size of the batches used when loading entities and collections.

-1 means batch loading is disabled. This is the default.

int-1

quarkus.hibernate-orm.statistics

Whether statistics collection is enabled.

booleanfalse
Query related configurationTypeDefault

quarkus.hibernate-orm.query.query-plan-cache-max-size

The maximum size of the query plan cache.

string

quarkus.hibernate-orm.query.default-null-ordering

Default precedence of null values in ORDER BY clauses.

Valid values are: none, first, last.

string
Database related configurationTypeDefault

quarkus.hibernate-orm.database.generation

Select whether the database schema is generated or not. drop-and-create is awesome in development mode. Accepted values: none, create, drop-and-create, drop, update.

stringnone

quarkus.hibernate-orm.database.generation.halt-on-error

Whether we should stop on the first error when applying the schema.

booleanfalse

quarkus.hibernate-orm.database.default-catalog

The default catalog to use for the database objects.

string

quarkus.hibernate-orm.database.default-schema

The default schema to use for the database objects.

string

quarkus.hibernate-orm.database.charset

The charset of the database.

string
JDBC related configurationTypeDefault

quarkus.hibernate-orm.jdbc.timezone

The time zone pushed to the JDBC driver.

string

quarkus.hibernate-orm.jdbc.statement-fetch-size

How many rows are fetched at a time by the JDBC driver.

int

quarkus.hibernate-orm.jdbc.statement-batch-size

The number of updates (inserts, updates and deletes) that are sent by the JDBC driver at one time for execution.

int
Logging configurationTypeDefault

quarkus.hibernate-orm.log.sql

Show SQL logs and format them nicely. Setting it to true is obviously not recommended in production.

booleanfalse

quarkus.hibernate-orm.log.jdbc-warnings

Whether JDBC warnings should be collected and logged.

booleandepends on dialect
Caching configurationTypeDefault

quarkus.hibernate-orm.cache."cache".expiration.max-idle

The maximum time before an object of the cache is considered expired.

Duration

quarkus.hibernate-orm.cache."cache".memory.object-count

The maximum number of objects kept in memory in the cache.

long

About the Duration formatThe format for durations uses the standard java.time.Duration format.You can learn more about it in the Duration#parse() javadoc.You can also provide duration values starting with a number.In this case, if the value consists only of a number, the converter treats the value as seconds.Otherwise, PT is implicitly prepended to the value to obtain a standard java.time.Duration format.
Do not mix persistence.xml and quarkus.hibernate-orm.* properties in application.properties.Quarkus will raise an exception.Make up your mind on which approach you want to use.
Want to start a PostgreSQL server on the side with Docker?
  1. docker run ulimit memlock=-1:-1 -it rm=true memory-swappiness=0 \ name postgres-quarkus-hibernate -e POSTGRES_USER=hibernate \ -e POSTGRES_PASSWORD=hibernate -e POSTGRES_DB=hibernate_db \ -p 5432:5432 postgres:10.5

Setting up and configuring Hibernate ORM with a persistence.xml

Alternatively, you can set a META-INF/persistence.xml to setup Hibernate ORM.This is useful for:

  • migrating existing code

  • when you have relatively complex settings requiring the full flexibility of the configuration

  • or if you like it the good old way

If you have a persistence.xml, then you cannot use the quarkus.hibernate-orm.* propertiesand only persistence units defined in persistence.xml will be taken into account.

Your pom.xml dependencies as well as your Java code would be identical to the precedent example. The onlydifference is that you would specify your Hibernate ORM configuration in META-INF/persistence.xml:

  1. <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  4. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  5. version="2.1">
  6. <persistence-unit name="CustomerPU" transaction-type="JTA">
  7. <description>My customer entities</description>
  8. <properties>
  9. <!-- Connection specific -->
  10. <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
  11. <property name="hibernate.show_sql" value="true"/>
  12. <property name="hibernate.format_sql" value="true"/>
  13. <!--
  14. Optimistically create the tables;
  15. will cause background errors being logged if they already exist,
  16. but is practical to retain existing data across runs (or create as needed) -->
  17. <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
  18. <property name="javax.persistence.validation.mode" value="NONE"/>
  19. </properties>
  20. </persistence-unit>
  21. </persistence>

Defining entities in external projects or jars

Hibernate ORM in Quarkus relies on compile-time bytecode enhancements to your entities. If you define your entities in thesame project where you build your Quarkus application, everything will work fine. If the entities come from external projectsor jars, you can make sure that your jar is treated like a Quarkus application library by adding an empty META-INF/beans.xml file.This will allow Quarkus to index and enhance your entities as if they were inside the current project.

Hibernate ORM in development mode

Quarkus development 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 import.sql.That way for every change to your app and in particular to your entities, the database schema will be properly recreatedand 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 reload 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 /import.sql file (if present).You can change the file name by changing the property quarkus.hibernate-orm.sql-load-script in application.properties.

The second approach is to use quarkus.hibernate-orm.database.generation=update.This approach is best when you do many entity changes butstill need to work on a copy of the production dataor 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 situationsincluding 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 quarkus.hibernate-orm.database.generation=none.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 quarkus.hibernate-orm.database.generation drop-and-create and update in your production environment. You have been warned :)

It becomes really powerful when combined with Quarkus configuration profiles.You can define different configuration profilesto 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.

application.properties

  1. %dev.quarkus.hibernate-orm.database.generation = drop-and-create
  2. %dev.quarkus.hibernate-orm.sql-load-script = import-dev.sql
  3. %dev-with-data.quarkus.hibernate-orm.database.generation = update
  4. %dev-with-data.quarkus.hibernate-orm.sql-load-script = no-file
  5. %prod.quarkus.hibernate-orm.database.generation = none
  6. %prod.quarkus.hibernate-orm.sql-load-script = no-file
  1. ./mvnw compile quarkus:dev -Dquarkus.profile=dev-with-data

Caching

Applications that frequently read the same entities can see their performance improved when the Hibernate ORM second-level cache is enabled.

Caching of entities

To enable second-level cache, mark the entities that you want cached with @javax.persistence.Cacheable:

  1. @Entity
  2. @Cacheable
  3. public class Country {
  4. int dialInCode;
  5. // ...
  6. }

When an entity is annotated with @Cacheable, all its field values are cached except for collections and relations to other entities.

This means the entity can be loaded without querying the database, but be careful as it implies the loaded entity might not reflect recent changes in the database.

Caching of collections and relations

Collections and relations need to be individually annotated to be cached; in this case the Hibernate specific @org.hibernate.annotations.Cache should be used, which requires also to specify the CacheConcurrencyStrategy:

  1. package org.acme;
  2. @Entity
  3. @Cacheable
  4. public class Country {
  5. // ...
  6. @OneToMany
  7. @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
  8. List<City> cities;
  9. // ...
  10. }

Caching of queries

Queries can also benefit from second-level caching. Cached query results can be returned immediately to the caller, avoiding to run the query on the database.

Be careful as this implies the results might not reflect recent changes.

To cache a query, mark it as cacheable on the Query instance:

  1. Query query = ...
  2. query.setHint("org.hibernate.cacheable", Boolean.TRUE);

If you have a NamedQuery then you can enable caching directly on its definition, which will usually be on an entity:

  1. @Entity
  2. @NamedQuery(name = "Fruits.findAll",
  3. query = "SELECT f FROM Fruit f ORDER BY f.name",
  4. hints = @QueryHint(name = "org.hibernate.cacheable", value = "true") )
  5. public class Fruit {
  6. ...

That’s all! Caching technology is already integrated and enabled by default in Quarkus, so it’s enough to set which ones are safe to be cached.

Tuning of Cache Regions

Caches store the data in separate regions to isolate different portions of data; such regions are assigned a name, which is useful for configuring each region independently, or to monitor their statistics.

By default entities are cached in regions named after their fully qualified name, e.g. org.acme.Country.

Collections are cached in regions named after the fully qualified name of their owner entity and collection field name, separated by # character, e.g. org.acme.Country#cities.

All cached queries are by default kept in a single region dedicated to them called default-query-results-region.

All regions are bounded by size and time by default. The defaults are 10000 max entries, and 100 seconds as maximum idle time.

The size of each region can be customized via the quarkus.hibernate-orm.cache."<regionname>".memory.object-count property (Replace <regionname> with the actual region name).

To set the maximum idle time, provide the duration (see note on duration’s format below) via the quarkus.hibernate-orm.cache."<regionname>".expiration.max-idle property (Replace <regionname> with the actual region name).

The double quotes are mandatory if your region name contains a dot. For instance:
  1. quarkus.hibernate-orm.cache."org.acme.MyEntity".memory.object-count=1000
The format for durations uses the standard java.time.Duration format.You can learn more about it in the Duration#parse() javadoc.You can also provide duration values starting with a number.In this case, if the value consists only of a number, the converter treats the value as seconds.Otherwise, PT is implicitly prepended to the value to obtain a standard java.time.Duration format.

Limitations of Caching

The caching technology provided within Quarkus is currently quite rudimentary and limited.

The team thought it was better to have some caching capability to start with, than having nothing; you can expect better caching solution to be integrated in future releases, and any help and feedback in this area is very welcome.

These caches are kept locally, so they are not invalidated or updated when changes are made to the persistent store by other applications.Also, when running multiple copies of the same application (in a cluster, for example on Kubernetes/OpenShift), caches in separate copies of the application aren’t synchronized.For these reasons, enabling caching is only suitable when certain assumptions can be made: we strongly recommend that only entities, collections and queries which never change are cached. Or at most, that when indeed such an entity is mutated and allowed to be read out of date (stale) this has no impact on the expectations of the application.Following this advice guarantees applications get the best performance out of the second-level cache and yet avoid unexpected behaviour.On top of immutable data, in certain contexts it might be acceptable to enable caching also on mutable data; this could be a necessary tradeoff on selected entities which are read frequently and for which some degree of staleness is acceptable; this " acceptable degree of staleness" can be tuned by setting eviction properties. This is however not recommended and should be done with extreme care, as it might produce unexpected and unforeseen effects on the data.Rather than enabling caching on mutable data, ideally a better solution would be to use a clustered cache; however at this time Quarkus doesn’t provide any such implementation: feel free to get in touch and let this need known so that the team can take this into account.

Finally, the second-level cache can be disabled globally by setting hibernate.cache.use_second_level_cache to false; this is a setting that needs to be specified in the persistence.xml configuration file.

When second-level cache is disabled, all cache annotations are ignored and all queries are run ignoring caches; this is generally useful only to diagnose issues.

Simplifying Hibernate ORM with Panache

The Hibernate ORM with Panache extension facilitates the usage of Hibernate ORM by providing active record style entities (and repositories) and focuses on making your entities trivial and fun to write in Quarkus.