Hibernate Reactive

https://zbciok.github.io/docs/smallrye-mutiny/implementations/hibernate-reactive

https://hibernate.org/reactive/

Reactive Object/Relational Mapping

Hibernate Reactive is a reactive API for Hibernate ORM, supporting non-blocking database drivers and a reactive style of interaction with the database.

Hibernate Reactive is intended for use in a reactive programming environment like Vert.x or Quarkus, where interaction with the database should occur in a non-blocking fashion. Persistence operations are orchestrated via the construction of a reactive stream rather than via direct invocation of synchronous functions in procedural Java code.

JDBC, JPA and Hibernate ORM use blocking IO for interaction with the database, and are therefore not appropriate for use in a reactive environment. Hibernate Reactive is a feature rich ORM implementation designed to take advantage of non-blocking database clients.

Compatibility

Hibernate Reactive requires:

  • Java 11,
  • Hibernate ORM 6.2, and
  • the Vert.x 4.4 reactive database client for your database.

It works with PostgreSQL, MySQL, MariaDB, Db2, SQL Server, Oracle and CockroachDB.

Getting Started

The Introduction to Hibernate Reactive covers everything you need to know to get started, including:


Reactive Session

https://zbciok.github.io/docs/smallrye-mutiny/implementations/hibernate-reactive

The reactive Session is available in two flavours: the Stage.Session which uses Java’s CompletionStage as the async type, and a Mutiny.Session, which uses Red Hat’s Mutiny asynchronous type Uni.

  • Stage.Session and friends provide a reactive API based around Java’s CompletionStage, and
  • Mutiny.Session and friends provide an API based on Mutiny.

These are the most important operations on reactive streams that you’ll need all the time when working with Hibernate Reactive:

PurposeJava CompletionStageMutiny Uni
Chain non-blocking operationsthenCompose()chain()
Transform streamed itemsthenApply()map() and replaceWith()
Perform an action using streamed itemsthenAccept()invoke() and call()
Perform cleanup (similar to finally)whenComplete()eventually()

When we use the term reactive stream in this document, we mean:

  • a chain of CompletionStages, or
  • a chain of Mutiny Unis and Multis

that is built by the program in order to service a particular request, transaction, or unit of work.

Example: https://jreact.com/index.php/2023/11/16/hibernate-reactive-completionstage-and-mutiny-example/