Hibernate Reactive - Getting Started Guide
Hibernate Reactive - Getting Started Guide
Sign Up Now!
Contents [hide]
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 1/18
14/9/2021 Hibernate Reactive - Getting Started Guide
The only 2 dependencies you need are Hibernate Reactive and one of Vert.X
reactive database clients. When you add these dependencies, make sure that
Hibernate Reactive supports your chosen Vert.X version. Otherwise, you will get
some strange exceptions when executing your test cases.
In the examples of this post, I will use a PostgreSQL database and therefore use
Vert.X PostgreSQL client.
Spring Data JPA Course x
Price
1
2 Increases
<project>
... In
3
4
5
6
03 : 15 : 33 : 43
<dependencies>
D
<dependency> H M S
<groupId>org.hibernate.reactive</groupId>
7 <artifactId>hibernate-reactive-core</artifactId
8 Sign Up Now!
<version>${hibernate.version}</version>
9 </dependency>
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 2/18
14/9/2021 Hibernate Reactive - Getting Started Guide
10 <dependency>
11 <groupId>io.vertx</groupId>
12 <artifactId>vertx-pg-client</artifactId>
13 <version>${vertx.version}</version>
14 </dependency>
15 ...
16 </dependencies>
17 </project>
You can use almost the same persistence.xml configuration for your Hibernate
Reactive project as you use in your Hibernate ORM projects. The only difference is
that you need to reference
org.hibernate.reactive.provider.ReactivePersistenceProvider as your provider.
1 <persistence>
2 <persistence-unit name="my-persistence-unit">
3 <description>Hibernate Reactive configuration - tho
4 <provider>org.hibernate.reactive.provider.ReactiveP
5 <exclude-unlisted-classes>false</exclude-unlisted-c
6 <properties>
7 <property name="hibernate.jdbc.time_zone" value
8
9 <property name="javax.persistence.jdbc.driver"
10 <property name="javax.persistence.jdbc.url" val
11 <property name="javax.persistence.jdbc.user" va
12 <property name="javax.persistence.jdbc.password
13
14 <property name="javax.persistence.schema-genera
15 <property name="javax.persistence.sql-load-scri
16 </properties>
17 </persistence-unit>
18 </persistence>
Entity Mappings
Spring Data JPA Course x
Price
After you Increases In dependencies, you can start working on your entity
added the required
mappings. As described earlier, Hibernate Reactive is based on Hibernate ORM,
which implements the JPA D
03 : 15 : 33 : 43
specification.
HAs of now,M
Hibernate Reactive
S supports
most of Hibernate ORM’s mapping annotations, and you can reference my previous
articles about Hibernate and JPA, ifSign
you have any questions.
Up Now!
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 3/18
14/9/2021 Hibernate Reactive - Getting Started Guide
Commonly used annotations that are currently not supported are @ManyToMany
and @ElementCollection. But that’s not a huge issue. I recommend avoiding
@ElementCollection in general. And instead of a many-to-many association, you
can map the association table to its own entity class with 2 many-to-one
associations.
All entities need to fulfill the JPA requirements. Here you can see an example of an
entity that maps information in the ChessPlayer database table. It maps the first
and last name of the player, their date of birth, and 2 associations to the games
they played as white and black. The mapping annotations on the id attribute mark
it as the primary key attribute and tell Hibernate to use a database sequence to
generate unique primary key values.
1 @Entity
2 public class ChessPlayer {
3
4 @Id
5 @GeneratedValue(strategy = GenerationType.SEQUENCE, gen
6 @SequenceGenerator(name = "player_seq", sequenceName =
7 private Long id;
8
9 private String firstName;
10
11 private String lastName;
12
13 private LocalDate birthDate;
14
15 @OneToMany(mappedBy = "playerWhite")
16 private Set<ChessGame> gamesWhite;
17
18 @OneToMany(mappedBy = "playerBlack")
19 private Set<ChessGame> gamesBlack;
Spring Data
20
21 JPA Course
@Version x
Price Increases
22
23 In
private int version;
31
32 public void setFirstName(String firstName) {
33 this.firstName = firstName;
34 }
35
36 // more getter and setter methods
37 }
Based on your entity mappings, you can then implement your database access.
Similar to the entity mapping annotations, Hibernate Reactive adapted the
SessionFactoy, Session, and Query interfaces. You can decide if you want to use
them based on the Mutiny or CompletionStage API. In the following sections, I will
provide you an example for both APIs.
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 5/18
14/9/2021 Hibernate Reactive - Getting Started Guide
2 Ebooks
to boost your Hibernate skills
Subscribe
I will collect, use and protect your data in accordance with my Privacy
Spring Data JPA Coursepolicy. x
Price Increases In
03 D : 15 H : 33 M : 43 S
Getting a Hibernate SessionFactory
Sign Up Now!
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 6/18
14/9/2021 Hibernate Reactive - Getting Started Guide
As explained earlier, the configuration is based on JPA, and you can use the
standard JPA APIs to get your EntityManagerFactory. In the next step, you need to
decide which API you want to use and unwrap a Mutiny.SessionFactory:
or a Stage.SessionFactory:
In the next step, you can then use the SessionFactory to get a Session and execute
your operations. The reactive Session interfaces offer the same methods that you
know from Hibernate ORM.
Mutiny API
CompletionStage API
Sign Up Now!
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 7/18
14/9/2021 Hibernate Reactive - Getting Started Guide
Querying Entities
Using Hibernate Reactive, you can query your entities in the same way you do with
Hibernate ORM. You can call the find method on your Session to get an entity by its
primary key or write queries using JPQL, Criteria API, and native SQL statements.
Mutiny API
1 factory.withSession(
2 session -> session.find(ChessGame.class, 1L)
3 ).await().indefinitely();
CompletionStage API
1 factory.withSession(
2 session -> session.find(ChessGame.class, 1L)
3 ).toCompletableFuture().join();
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 8/18
14/9/2021 Hibernate Reactive - Getting Started Guide
Let’s use a JOIN FETCH clause first. It’s the recommended approach because it
provides better performance than the programmatic initialization and is easier to
define than an EntityGraph.
Mutiny API
CompletionStage API
Programmatic Initialization
If you already retrieved an entity from the database and want to fetch an
association programmatically, you need to call the Session.fetch method.
8
9
10 ).await().indefinitely();
CompletionStage API
Updating Entities
The easiest way to update entity objects is to query them from the database and
call one or more of their setter methods. When you do that, make sure to call the
withTransaction method on your SessionFactory to get a Session instance with an
associated transaction. Hibernate Reactive will then handle the required flush and
commit operations for you.
Mutiny API
Price Increases In
CompletionStage API
1
2
3
03 : 15 : 33 : 43
factory.withTransaction((session, tx) -> session.createQuery
D H M
.getResultList() S
.thenAccept(players -> players.forEa
4 ).toCompletableFuture().join();
Sign Up Now!
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 10/18
14/9/2021 Hibernate Reactive - Getting Started Guide
Removing Entities
And you can remove entities in a similar way. You get a Session instance with an
associated transaction, query the entities from the database and call the
Session.remove method for each of them.
Mutiny API
CompletionStage API
Conclusion
If you’re already familiar with JPA and Hibernate ORM and want to implement a
reactive application, then you should try Hibernate Reactive. It enables you to use
the same mapping annotations and APIs that you already know from Hibernate
ORM.
There are also several differences, like the fetching of lazy associations and the
TAGS
Sign Up Now!
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 11/18
14/9/2021 Hibernate Reactive - Getting Started Guide
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 12/18
14/9/2021 Hibernate Reactive - Getting Started Guide
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 13/18
14/9/2021 Hibernate Reactive - Getting Started Guide
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 14/18
14/9/2021 Hibernate Reactive - Getting Started Guide
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 15/18
14/9/2021 Hibernate Reactive - Getting Started Guide
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 16/18
14/9/2021 Hibernate Reactive - Getting Started Guide
Leave a Reply
Your email address will not be published. Required fields are marked
Comment
Name *
Email *
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 17/18
14/9/2021 Hibernate Reactive - Getting Started Guide
Save my name, email, and website in this browser for the next time I comment.
POST COMMENT
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Jachu
Is hibernate-reactive supported by Spring Data as well ?
Reply
Thorben Janssen
No, it isn’t. The Spring team has its own library for reactive database access
(Spring Data R2DBC) and (as far as I know) has no immediate plans to
support another one.
Regards,
Thorben
Reply
https://thorben-janssen.com/hibernate-reactive-getting-started-guide/ 18/18