Hibernate Reactive 1.0.3.final Reference Documentation
Hibernate Reactive 1.0.3.final Reference Documentation
Table of Contents
Preface
1. Introduction to Hibernate Reactive
1.1. Information about Hibernate ORM
1.2. Setting up a reactive Hibernate project
1.2.1. Including Hibernate Reactive in your project build
1.2.2. Optional dependencies
1.2.3. Basic configuration
1.2.4. Automatic schema export
1.2.5. Logging the generated SQL
1.2.6. Minimizing repetitive mapping information
1.3. Writing the Java code
1.3.1. Mapping entity classes
1.3.2. Getters and setters
1.3.3. equals() and hashCode()
1.3.4. Identifier generation
1.3.5. Custom types
1.3.6. Attribute converters
1.3.7. APIs for chaining reactive operations
1.3.8. Obtaining a reactive session factory
1.3.9. Obtaining a reactive session
1.3.10. Using the reactive session
1.3.11. Queries
1.3.12. Fetching lazy associations
1.3.13. Field-level lazy fetching
1.3.14. Transactions
1.4. Integrating with Vert.x
1.4.1. Sessions and Vert.x contexts
1.4.2. Executing code in a Vert.x context
1.4.3. Vert.x instance service
1.5. Tuning and performance
1.5.1. Tuning the Vert.x pool
1.5.2. Enabling statement batching
1.5.3. Association fetching
1.5.4. Enabling the second-level cache
1.5.5. Session cache management
1.5.6. Stateless sessions
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 1/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Preface
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, 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. The reactive stream is represented
using a chain of Java CompletionStage s or Mutiny Uni s and Multi s.
Java persistence frameworks like JDBC, JPA and Hibernate ORM were designed to use blocking IO for interaction with the
database, and are therefore not appropriate for use in a reactive environment. As far as we know, Hibernate Reactive is the
first true ORM implementation designed to take advantage of non-blocking database clients. Out of the box, the Vert.x clients
for PostgreSQL, MySQL, and DB2 are supported, though the architecture is not limited to these drivers.
This programming paradigm holds the potential for improved scalability, and more controlled degradation under peak load,
in some runtime scenarios. However, in general, one should not expect to see an immediate performance gain in all
performance tests. Indeed, many programs will not benefit from the programming model, and those which do benefit might
only benefit in very specific load scenarios.
writing Java code to define a data model and access the database.
Last, we’ll discuss some topics related to performance that you’ll need to understand when you develop any large-scale
project using Hibernate.
But, before you start, we recommend taking a quick look at the very simplistic example program in the session-
example directory, which shows off all the "bits" you’ll need to get your own program up and running.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 2/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Java Persistence with Hibernate, the latest edition of the book originally titled Hibernate in Action.
include Hibernate Reactive itself, along with the appropriate Vert.x reactive database client, as dependencies of your
project, and
configure Hibernate Reactive with information about your database, using Hibernate configuration properties.
Or, if you want to use Hibernate Reactive in Quarkus, you can generate a preconfigured skeleton project right here.
org.hibernate.reactive:hibernate-reactive-core:{version}
You’ll also need to add a dependency for the Vert.x reactive database driver for your database, one of the following options:
DB2 io.vertx:vertx-db2-client:{vertxVersion}
Where {vertxVersion} is the version of Vert.x compatible with the version of Hibernate Reactive you’re using.
You don’t need to depend on the JDBC driver for your database.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 3/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
You might also add the Hibernate bytecode enhancer to your Gradle build if you want to use field-level lazy fetching.
Field-level lazy fetching is an advanced feature that most programs don’t need. Stick to the basics for now.
The only required configuration that’s really specific to Hibernate Reactive is the persistence <provider> element, which
must be explicit:
<provider>org.hibernate.reactive.provider.ReactivePersistenceProvider</provider>
Otherwise, configuration is almost completely transparent—you can configure Hibernate Reactive pretty much exactly as
you would usually configure Hibernate ORM core.
A full list of configuration properties recognized by Hibernate may be found in the documentation for Hibernate ORM.
You’ll never need to touch most of these. The properties you do need at this stage are these three:
These configuration properties have jdbc in their names, but of course there’s no JDBC in Hibernate Reactive, and these
are simply the legacy property names defined by the JPA specification. In particular, Hibernate Reactive itself parses and
interprets the JDBC URL.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 4/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
You don’t need to specify hibernate.dialect . The correct Hibernate Dialect will be determined
for you by Hibernate Reactive.
The Vert.x database client has built-in connection pooling and prepared statement caching. You might want to control the size
of the connection pool:
We’ll learn about more advanced connection pool tuning later, in Tuning the Vert.x pool.
Hibernate has many configurable things, but many exist only to maintain compatibility with legacy code,
and most configuration properties directly related to JDBC or JTA aren’t relevant in the context of
Hibernate Reactive.
javax.persistence.schema- If create , first drop the schema and then export tables, sequences, and
generation.database.action constraints.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 5/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Schema export uses blocking operations so starting the factory might require special handling when using it. Failing to do so
will cause an exception:
emf = Persistence
.createEntityManagerFactory("demo")
.unwrap(Mutiny.SessionFactory.class);
return Uni.createFrom().voidItem();
});
startHibernate = vertx.executeBlocking(startHibernate)
enable debug-level logging for the category org.hibernate.SQL using your preferred SLF4J logging implementation.
For example, if you’re using Log4J 2 (as above in Optional dependencies), add these lines to
your log4j2.properties file:
logger.hibernate.name = org.hibernate.SQL
logger.hibernate.level = debug
You can make the logged SQL more readable by enabling one or both of the following settings:
hibernate.highlight_sql If true, log SQL with syntax highlighting via ANSI escape codes
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 6/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
The following properties are very useful for minimizing the amount of information you’ll need to explicitly specify
in @Table and @Column annotations which we’ll discuss below in Mapping entity classes:
hibernate.default_schema A default schema name for entities which do not explicitly declare one
hibernate.default_catalog A default catalog name for entities which do not explicitly declare one
Writing your own PhysicalNamingStrategy is an especially good way to reduce the clutter of
annotations on your entity classes, and we think you should do it for any nontrivial data model.
As is the case in any project that uses Hibernate, your persistence-related code comes in two main pieces:
1. a representation of your data model in Java, which takes the form of a set of annotated entity classes, and
2. a larger number of functions which interact with Hibernate’s APIs to perform the persistence operations associated with
your various transactions.
The first part, the data or "domain" model, is usually easier to write, but doing a great and very clean job of it will strongly
affect your success in the second part.
Take your time with this code, and try to produce a Java model that’s as close as reasonable to the
relational data model. Avoid using exotic or advanced mapping features when they’re not really needed.
When in the slightest doubt, map a foreign key relationship
using @ManyToOne with @OneToMany(mappedBy=…
) in preference to more complicated association
mappings.
The second part of the code is much trickier to get right. This code must:
construct reactive streams by chaining persistence operations invoked on the reactive session,
handle failures.
Some responsibility for transaction and session management, and for recovery from certain kinds of
failure, can be best handled in some sort of framework code.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 7/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
For example:
@Entity
@Table(name="authors")
class Author {
@Id @GeneratedValue
@NotNull @Size(max=100)
Author(String name) {
this.name = name;
Author() {}
the regular JPA mapping annotations defined in the package javax.persistence with
A full list of object/relational mapping annotations may be found in the documentation for Hibernate ORM. Most mapping
annotations are already supported in Hibernate Reactive, though there are still a handful of limitations at this time.
Annotation Purpose
@Entity Declares an entity class (a class with its own database table an persistent identity)
@Embeddable or @Embedded Declare an embeddable class (a class without its own persistent identity or database
table)
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 8/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Annotation Purpose
@Id Specifies that a field of an entity holds the persistent identity of the entity, and maps to
the primary key of its table
@IdClass Specifies a class representing the composite primary key of the entity (for entities with
multiple @Id fields)
@EmbeddedId Specifies that a field of an entity holds its composite primary key represented as
an @Embeddable class
@Version Specifies that a field of an entity holds a version number used for optimistic locking
Annotation Purpose
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 9/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Annotation Purpose
@DynamicInsert and @DynamicUpdate Generate SQL dynamically with only needed columns (instead of
using static SQL generated at startup)
for defining a required field, we prefer to use the @NotNull annotation from Bean Validation instead of
JPA’s more verbose @Basic(optional=false) . Similarly, we prefer to define the length of a text field
using @Size(100) rather than @Column(length=100) .
a nullary constructor.
It’s illegal to access persistent fields from outside the entity class. Therefore, external access to persistent fields must be
intermediated via getter and setter methods defined by the entity class.
If you access fields of an unfetched entity instance from code outside the entity class, you’ll obtain
bogus null or default (zero) values!
When you use Hibernate Reactive in Quarkus, these requirements are relaxed, and you can use public fields instead of getters
and setters if you prefer.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 10/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
You should not include mutable fields in the hashcode, since that would require rehashing any collection containing the
entity whenever the field is mutated.
It’s not completely wrong to include a generated identifier (surrogate key) in the hashcode, but since the identifier is not
generated until the entity instance is made persistent, you must take great care to not add it to any hashed collection
before the identifier is generated. We therefore advise against including any database-generated field in the hashcode.
We therefore recommend identifying a natural key for each entity, that is, a combination of fields that
uniquely identifies an instance of the entity, from the perspective of the data model of the program. The
business key should correspond to a unique constraint on the database, and to the fields which are
included in equals() and hashCode() .
That said, an implementation of equals() and hashCode() based on the generated identifier of the entity can work if
you’re careful.
If you can’t identify a natural key, it might be a sign that you need to think more carefully about some
aspect of your data model. If an entity doesn’t have a meaningful unique key, then it’s impossible to say
what event or object it represents in the "real world" outside your program.
Note that even when you’ve identified a natural key, we still recommend the use of a generated surrogate key in foreign keys,
since this makes your data model much easier to change.
Sequence, table, and UUID id generation is built in, and these id generation strategies may be selected using the usual
JPA mapping annotations: @GeneratedValue , @TableGenerator , @SequenceGenerator .
Custom id generators may be defined by implementing ReactiveIdentifierGenerator and declaring the custom
implementation using @GenericGenerator .
Natural ids—including composite ids—may be assigned by the program in the usual way.
The standard id generation strategies defined by the JPA specification may be customized using the following annotations:
Annotation Purpose
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 11/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Annotation Purpose
@Entity
@Table(name="authors")
class Author {
sequenceName = "author_ids",
allocationSize = 20)
Integer id;
...
If you have very particular requirements, you can check out the Javadoc of ReactiveIdentifierGenerator for
information on how to implement your own custom reactive identifier generator.
Therefore, some existing UserType implementations will work in Hibernate Reactive, depending upon precisely which
features of JDBC they depend on.
Where possible, use a JPA attribute converter instead of a custom type, since attribute converters are not in
any way tied to JDBC.
You may specify a custom type by annotating a field of an entity class with the Hibernate @Type annotation.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 12/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
@Converter
@Override
@Override
Annotation Purpose
You’ll find more information in the Javadoc for these annotations and in the JPA specification.
Stage.Session and friends provide a reactive API based around Java’s CompletionStage , and
If you take the time to look over the types Stage.Session and Mutiny.Session , you’ll notice
they’re almost identical. Choosing between them is a matter of deciding which reactive API you want to
use for working with reactive streams. Your decision won’t affect what you can do with Hibernate
Reactive. On the other hand, we’ve sent a lot of feedback and requests for improvement to the Mutiny
team, and we think it’s now the case that Hibernate Reactive code is simpler and cleaner with Mutiny.
These are the most important operations on reactive streams that you’ll need all the time when working with Hibernate
Reactive:
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 13/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
In this introduction, our code examples usually use Mutiny. If you’re more familiar with CompletionStage , you can refer
to the above table to help you understand the code.
that is built by the program in order to service a particular request, transaction, or unit of work.
Now, unwrap() the reactive SessionFactory . If you want to use CompletionStage s for chaining reactive operations,
ask for a Stage.SessionFactory :
Or, if you prefer to use the Mutiny-based API, unwrap() the type Mutiny.SessionFactory :
It’s also possible to construct a reactive SessionFactory via programmatic configuration based on
Hibernate’s ServiceRegistry architecture, by using a ReactiveServiceRegistryBuilder . But
that’s outside the scope of this document.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 14/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Also remember that a Hibernate session is a lightweight object that should be created, used, and then discarded within a
single logical unit of work.
That is to say, you should reuse the same session across multiple persistence operations within a single
reactive stream representing a certain transaction or unit of work, but don’t share a session between
different concurrent reactive streams!
sessionFactory.withSession(
.invoke(
);
The resulting Session object is automatically associated with the current reactive stream, and so nested calls
to withSession() in a given stream automatically obtain the same shared session.
Alternatively, you may use openSession() , but you must remember to close() the session when you’re done. And you
must take great care to only access each session from within exactly one Vert.x context. (See Sessions and Vert.x
contexts more on this).
sessionUni.chain(
.invoke(
.eventually(session::close)
);
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 15/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
If you’re not familiar with these operations, don’t despair! Their semantics are defined in the JPA specification, and in the
API documentation, and are explained in innumerable articles and blog posts. But if you already have some experience with
Hibernate or JPA, you’re right at home!
Just like in Hibernate ORM, the session is considered to be unusable after any of its methods throws an
exception. If you receive an exception from Hibernate Reactive, you should immediately close and discard
the current session.
Now, here’s where Hibernate Reactive is different: in the reactive API, each of these methods returns its result in a non-
blocking fashion via a Java CompletionStage (or Mutiny Uni ). For example:
session.find(Book.class, book.id)
On the other hand, methods with no meaningful return value just return CompletionStage<Void> (or Uni<Void> ).
session.find(Book.class, id)
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 16/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
The session will be flushed automatically at the end of a unit of work if—and only if—you use a
transaction, as described below in Transactions. If you don’t use a transaction, and forget to flush the
session explicitly, your persistence operations might never be sent to the database!
An extremely common mistake when using reactive streams is to forget to chain the return value of a "void-like" method. For
example, in the following code, the flush() operation is never executed, because invoke() doesn’t chain its return value
to the tip of the stream.
session.find(Book.class, id)
So remember:
You must use thenCompose() , not thenAccept() , when calling "void-like" methods that
return CompletionStage .
In Mutiny, you must use call() , not invoke() , when calling "void-like" methods that return Uni .
The same problem occurs in the following code, but this time it’s remove() that never gets called:
session.find(Book.class, id)
return session.flush();
} )
If you already have some experience with reactive programming, there’s nothing new to learn here. But if you are new to
reactive programming, just be aware that you’re going to make this mistake, in some form, at least once!
1.3.11. Queries
Naturally, the Session interface is a factory for Query instances which allow you to set query parameters and execute
queries and DML statements:
createQuery() Obtain a Query for executing a query or DML statement written in HQL or JPQL
createNativeQuery() Obtain a Query for executing a query or DML statement written in the native SQL dialect of
your database
createNamedQuery() Obtain a Query for executing a named HQL or SQL query defined by
a @NamedQuery annotation
That createQuery() method produces a reactive Query , allowing HQL / JPQL queries to be executed asynchronously,
always returning their results via a CompletionStage (or Uni ):
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 17/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
.getResultList()
setFirstResult() Specify a certain number of initial results to be skipped (for result pagination)
executeUpdate() Execute a DML statement and obtain the number of affected rows
The Hibernate Reactive Query API doesn’t support java.util.Date or its subclasses in java.sql ,
nor java.util.Calendar . Always use java.time types like LocalDate or LocalDateTime for
specifying arguments to temporally-typed query parameters.
For JPA criteria queries, you must first obtain the CriteriaBuilder using SessionFactory.getCriteriaBuilder() ,
and execute your query using Session.createQuery() .
Root<Author> a = query.from(Author.class);
Join<Author,Book> b = a.join(Author_.books);
query.select(b);
return session.createQuery(query).getResultList().invoke(
);
Therefore, lazy fetching is an explicit operation named fetch() , a static method of Stage and Mutiny :
session.find(Author.class, author.id)
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 18/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
It’s very important to make sure you’ve fetched all the data that will be needed before passing control to
the process that renders the UI! There is no transparent lazy fetching in Hibernate Reactive, so patterns
like "open session in view" will not help at all.
Sometimes you might need to chain multiple calls to fetch() , for example:
Mutiny.fetch( session.getReference(detachedAuthor) )
fetch() isn’t recursive! You can’t fetch an association belonging to an unfetched entity without fetching
the entity instance first.
session.find(Book.class, book.id)
We don’t encourage you to use field-level lazy fetching unless you have very specific requirements.
1.3.14. Transactions
The withTransaction() method performs work within the scope of a database transaction.
For a given Session object, nested calls to withTransaction() occur within the same shared transaction context.
However, notice that the transaction is a resource local transaction only, delegated to the underlying Vert.x database client,
and does not span multiple datasources, nor integrate with JPA container-managed transactions.
For extra convenience, there’s a method that opens a session and starts a transaction in one call:
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 19/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
This is probably the most convenient thing to use most of the time.
Remember how in regular old Hibernate JPA, you’re not supposed to share a session between multiple
threads? Well, the idea here is essentially similar, it’s just that the notion of a "thread" is a little more
slippery, or at least more technical. You need to be able to replace the idea of a "thread" with the idea of a
chain of callbacks occurring on a reactive stream, all within the scope of a certain Vert.x local context.
When you create a session using withSession() or withTransaction() , it’s automatically associated with the current
Vert.x local context, and propagates with the local context, as mentioned above in Obtaining a reactive session. And you’re
only allowed to use the session from the thread that owns this local context. If you screw up, and use it from a different
thread, you might see this error:
HR000068: This method should exclusively be invoked from a Vert.x EventLoop thread; ...
On the other hand, if you use openSession() , you’ll have to manage the association between sessions and contexts
yourself. Now, that’s in principle straightforward, but you’d be surprised how often people mess up.
The session is not thread-safe (or "stream-safe"), so using it across different threads (or reactive streams)
may cause bugs that are extremely hard to detect. Don’t say we didn’t warn you!
For example, I bet you would like to be able to write code like this:
list.add(session.persist(entity));
CompletableFuture.allOf(list).thenCompose(session::flush);
Well, we’re sorry, but that’s just not allowed. Parallel reactive streams may not share a session. Each stream must have its
own session.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 20/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
});
Within the block of code passed to runOnContext() , you’ll be able to use the Hibernate Reactive session associated with
the context.
public MyVertx() {
this.vertx = Vertx.vertx();
@Override
return vertx;
One way to register this implementation is to configure Hibernate programmatically, for example:
.applySettings( configuration.getProperties() );
@Override
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 21/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
org.myproject.MyServiceContributor
Fortunately, most performance problems are relatively easy to solve with the tools that Hibernate makes available to you, as
long as you keep a couple of simple principles in mind.
First and most important: the reason you’re using Hibernate Reactive is because it makes things easier. If, for a certain
problem, it’s making things harder, stop using it. Solve this problem with a different tool instead.
Just because you’re using Hibernate in your program doesn’t mean you have to use it everywhere.
Second: there are two main potential sources of performance bottlenecks in a program that uses Hibernate:
So performance tuning primarily involves reducing the number of accesses to the database, and/or controlling the size of the
session cache.
But before we get to those more advanced topics, we should start by tuning the connection pool.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 22/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Finally, for more advanced cases, you can write your own code to configure the Vert.x client by
implementing SqlClientPoolConfiguration .
(Again, this property has jdbc in its name, but Hibernate Reactive repurposes it for use with the reactive connection.)
Even better than DML statement batching is the use of HQL update or delete queries, or even native
SQL that calls a stored procedure!
explicitly specify all the data you’re going to need right at the start of a session/transaction, and fetch it immediately in
one or two queries,
Without question, the most common cause of poorly-performing data access code in Java programs is the problem of N+1
selects. Here, a list of N rows is retrieved from the database in an initial query, and then associated instances of a related
entity are fetched using N subsequent queries.
Hibernate code which does this is bad code and makes Hibernate look bad to people who don’t realize that
it’s their own fault for not following the advice in this section!
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 23/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Hibernate provides several strategies for efficiently fetching associations and avoiding N+1 selects:
subselect fetching.
Of these, you should almost always use outer join fetching. Batch fetching and subselect fetching are only useful in rare cases
where outer join fetching would result in a cartesian product and a huge result set. Unfortunately, outer join fetching simply
isn’t possible with lazy fetching.
Avoid the use of lazy fetching, which is often the source of N+1 selects.
It follows from this tip that you shouldn’t need to use Stage.fetch() or Mutiny.fetch() very often!
Now, we’re not saying that associations should be mapped for eager fetching by default! That would be a terrible idea,
resulting in simple session operations that fetch the entire database! Therefore:
It sounds as if this tip is in contradiction to the previous one, but it’s not. It’s saying that you must explicitly specify eager
fetching for associations precisely when and where they are needed.
a fetch profile,
a JPA EntityGraph , or
You can find much more information about association fetching in the documentation for Hibernate ORM.
Hibernate Reactive supports second-level cache implementations that perform no blocking I/O.
Make sure you disable any disk-based storage or distributed replication used by your preferred cache
implementation. A second-level cache which uses blocking I/O to interact with the network or disk-based
storage will at least partially negate the advantages of the reactive programming model.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 24/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
Configuring Hibernate’s second-level cache is a rather involved topic, and quite outside the scope of this document. But in
case it helps, we’re testing Hibernate Reactive with the following configuration, which uses EHCache as the cache
implementation, as above in Optional dependencies:
hibernate.cache.use_second_level_cache true
hibernate.cache.region.factory_class org.hibernate.cache.jcache.JCacheRegionFactory
hibernate.javax.cache.provider org.ehcache.jsr107.EhcacheCachingProvider
hibernate.javax.cache.uri /ehcache.xml
If you’re using EHCache, you’ll also need to include an ehcache.xml file that explicitly configures the behavior of each
cache region belonging to your entities and collections.
Don’t forget that you need to explicitly mark each entity that will be stored in the second-level cache with
the @Cache annotation from org.hibernate.annotations .
You can find much more information about the second-level cache in the documentation for Hibernate ORM.
The methods detach() and clear() allow you to remove entities from the session cache, making them available for
garbage collection. Since most sessions are rather short-lived, you won’t need these operations very often. And if you find
yourself thinking you do need them in a certain situation, you should strongly consider an alternative solution: a stateless
session.
Stage.StatelessSession ss = getSessionFactory().openStatelessSession();
A stateless session:
doesn’t have a first-level cache (persistence context), nor does it interact with any second-level caches, and
doesn’t implement transactional write-behind or automatic dirty checking, so all operations are executed immediately
when they’re explicitly called.
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 25/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
For a stateless session, you’re always working with detached objects. Thus, the programming model is a bit different:
get(Class, Object) Obtain a detached object, given its type and its id, by executing a select
insert(Object) Immediately insert the state of the given transient object into the database
delete(Object) Immediately delete the state of the given detached object from the database
There’s no flush() operation, and so update() is always explicit.
In certain circumstances, this makes stateless sessions easier to work with, but with the caveat that a stateless session is much
more vulnerable to data aliasing effects, since it’s easy to get two non-identical Java objects which both represent the same
row of a database table.
If you use fetch() in a stateless session, you can very easily obtain two objects representing the same
database row!
In particular, the absence of a persistence context means that you can safely perform bulk-processing tasks without allocating
huge quantities of memory. Use of a StatelessSession alleviates the need to call:
Stateless sessions can be useful, but for bulk operations on huge datasets, Hibernate can’t possibly
compete with stored procedures!
When using a stateless session, you should be aware of the following additional limitations:
changes to @ManyToMany associations and @ElementCollection s cannot be made persistent, and
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 26/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
database-level pessimistic locking using the SQL for update syntax (or equivalent).
In the Hibernate community it’s much more common to use optimistic locking, and Hibernate makes that incredibly easy.
Where possible, in a multiuser system, avoid holding a pessimistic lock across a user interaction. Indeed,
the usual practice is to avoid having transactions that span user interactions. For multiuser systems,
optimistic locking is king.
That said, there is also a place for pessimistic locks, which can sometimes reduce the probability of transaction rollbacks.
Therefore, the find() , lock() , and refresh() methods of the reactive session accept an optional LockMode . You can
also specify a LockMode for a query. The lock mode can be used to request a pessimistic lock, or to customize the behavior
of optimistic locking:
READ An optimistic lock obtained implicitly whenever an entity is read from the
database using select
OPTIMISTIC An optimistic lock obtained when an entity is read from the database, and verified
using a select to check the version when the transaction completes
OPTIMISTIC_FORCE_INCREMENT An optimistic lock obtained when an entity is read from the database, and enforced
using an update to increment the version when the transaction completes
WRITE A pessimistic lock obtained implicitly whenever an entity is written to the database
using update or insert
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 27/28
7/31/22, 7:42 PM Hibernate Reactive 1.0.3.Final Reference Documentation
A common motivation for defining a custom pool is the need to support multitenancy. In a multitenant application, the
database or database schema depends on the current tenant identifier. The easiest way to set this up in Hibernate Reactive is
to extend DefaultSqlClientPool and override getTenantPool(String tenantId) .
For multitenancy, you’ll also need to set at least one of the following configuration properties defined by Hibernate ORM:
If you don’t provide a CurrentTenantIdentifierResolver , you can specify the tenant id explicitly when you
call openSession() , withSession() , or withTransaction() .
https://hibernate.org/reactive/documentation/1.0/reference/html_single/#_including_hibernate_reactive_in_your_project_build 28/28