You are on page 1of 13

14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Can you use Hibernate/EclipseLink/JPA for your microservice?


By Thorben Janssen

Hibernate, JPA

tweet share share share email


         
Spring Data JPA Course
Everyone is building microservices. For the last 1.5 years, it was the dominant x
Price Increases
architectural In software projects. And while a microservice
style for all new
architecture should make the implementation of each service easier, it also
introduces new challenges.D 03  : 15  H : 33  M : 20  S
That’s especially the case for the persistence layer. So, it’s no surprise that I had
Sign Up Now!
lots of discussions about implementing the persistence layer of a microservice. I
https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 1/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

also helped lots of teams who struggled with the consequences of the new
architectural style. In this article, I want to summarize some of the discussions and
provide answers to the most common questions.

Contents [hide]

1 Can you use Hibernate/EclipseLink/JPA in your microservice?


2 Basic rules for microservices
3 Sharing data between services
3.1 Reading data from multiple services
3.2 Updating data in multiple services
4 Conclusion

Can you use Hibernate/EclipseLink/JPA in your microservice?


But before we start, let’s address the elephant in the room and answer one of the
most heated discussion topics: Yes, JPA and all its implementations (Hibernate,
EclipseLink, OpenJPA, …) can be valid options to implement the persistence layer of
a microservice.

You shouldn’t ask if Hibernate is a good fit for a microservice architecture. You


should ask if its a good fit for the use cases you need to implement.

Internally, microservices aren’t that different to monoliths. OK, they are much
smaller, and communication between service can get incredibly complicated. But
that most often doesn’t affect your persistence layer.
Spring Data JPA Course x
Price Increases
Microservices still need toIn
store and read information. If you decide to store your
data in a relational database, Hibernate might be a good option to implement your
persistence layer. Before you 03  : 15  : 33  : 20 
D make theH Mshould askSyourself:
decision, you

Do most of your use cases require record-centric CRUD operations? If that’s the
Sign Up Now!
case, JPA is a good option to implement your persistence layer.
https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 2/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Do you need to implement mass insert or update operations? In this case,


Hibernate’s object-relational mapping might create more problems than it
solves. You should take a look at other frameworks, like jOOQ.

Do you need to implement a lot of complex queries? You can do that using JPA’s
native queries. But are other frameworks, like jOOQ, are better suited for these
use cases. You should either use these or integrate them with Hibernate.

OK, for the rest of this article, let’s assume you mostly need to implement record-
centric CRUD use cases. So, JPA and its implementations are a good fit for your
microservice.

Basic rules for microservices


If you decide to build a system of microservices and take full advantage of the
benefits of this architectural style, you need to follow a few basic rules:

Each service has to have its own database. If you break that rule, you introduce
dependencies between your services. That makes them harder to develop,
deploy and operate.

A service only knows its own database. You can’t define any entities, managed
associations, queries, foreign key constraints, stored procedures or database
trigger that use a table that belongs to another microservice. If you have to
reference any information that’s managed by a different service, you can only
store the id(s) of the associated object(s).

Each service uses and controls its own transactions. There are no transactions
Spring Data JPA Course
that span over multiple services. Otherwise, your microservice system becomes
x
Price Increases In
hard to scale.

03  D : 15  H : 33  M : 20  S


Whenever you update your microservice, you need to update its database
automatically. Flyway and Liquibase are powerful and easy to use frameworks to
implement automated, version-based
Sign Upupdate
Now!procedures.

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 3/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Sharing data between services


If you ever tried to implement a system of microservices, you know that the
aforementioned rules are much easier to proclaim then to implement. Especially
rule 2, and 3 can be hard to fulfill if you need to implement complex business logic.

2 Ebooks 
Spring Data JPA Course
Price Increases In
x

to boost your
03  DHibernate
: 15  H : 33 skills
M : 20 S
Sign up below to join my newsletter and to get your free
Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 4/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

ebooks:

Java 8 Support in Hibernate 5

Native Queries with Hibernate

Your email address

Subscribe

I will collect, use and protect your data in accordance with my Privacy
policy.

The easiest and often recommend approach to avoid these problems is to design a
self-sustained service which manages all the information they need. While I agree
with that recommendation, I also know that it’s not always possible to do that.
There might be good reasons to split something into multiple services, and
sometimes new features just don’t fit well into the existing design. In these
situations, you need to implement read or write operations that affect the data
managed by different services.

Reading data from multiple services


Spring Data JPA Course
There are 2 general ways to make sure that services stay independent of each otherx
Price
while Increases
performing In that require data managed by different
read operations
microservices:
03  D : 15  H : 33  M : 20  S
API Composition: Your client application or another microservice integrates
multiple services by calling their API and merging the results in memory.
Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 5/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Create a view database: You create an additional microservice which


implements its query on its own database. This database contains a copy of all
the information that you need to implement your query. This is used in the
CQRS pattern, but there are also other ways, e.g., change data capture, to share
the information between multiple services. I will show you a few examples in
future articles.

Updating data in multiple services

And if you need to implement a write operation that affects multiple microservices,
you can use the SAGA pattern and split the operation into multiple API calls. Each
of these API calls uses a local transaction. Because you can’t rely on a distributed
business transaction, you need to execute compensating operations whenever one
of the API calls fails. These compensating operations need to undo all changes
performed by the previous operations.

Conclusion

As long as the required use cases are a good fit for an object-relational mapping
framework, there shouldn’t be anything holding you back from using Hibernate,
EclipseLink or any other JPA implementation to create your microservice. Most of
the challenges you need to solve while implementing your persistence layer, are
caused by the architectural style and not by JPA.

That clearly shows that there is no free lunch in software development. If you want

Spring Data JPA Course


to benefit from the advantages of a microprofile architecture, you also need to
x
handle its downsides. One of the biggest challenges is querying data from multiple
Price Increases In
services and ensuring data consistency during write operations that affect multiple

03  D : 15  H : 33  M : 20  S


services. In the next articles, I will get into more details on the patterns and
possible solutions to these challenges.

tweet share share


Sign Up Now!
share email
         
https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 6/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Spring Data JPA Course x


Price Increases In
03  D : 15  H : 33  M : 20  S
Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 7/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

ABOUT THE AUTHOR

Thorben is an independent consultant, international speaker, and trainer specialized in


solving Java persistence problems with JPA and Hibernate.
He is also the author of Amazon’s bestselling book Hibernate Tips - More than 70
solutions to common Hibernate problems.

Books and Courses

Spring Data JPA Course x


Price Increases In
03  D : 15  H : 33  M : 20  S
Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 8/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Spring Data JPA Course x


Price Increases In
03  D : 15  H : 33  M : 20  S
Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 9/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Spring Data JPA Course x


Price Increases In
03  D : 15  H : 33  M : 20  S
Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 10/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Coaching and Consulting

Spring Data JPA Course x


Price Increases In
Leave a Reply
03  : 15  : 33  : 20 
Your email address will notDbe published.
H RequiredM S
fields are marked

Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 11/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Comment

Name *

Email *

Website

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.
Spring Data JPA Course x

Price Increases In
03  D : 15  H : 33  M : 20  S
Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 12/13
14/9/2021 Can you use Hibernate/EclipseLink/JPA in your microservice?

Copyright 2021 Thorben Janssen, all rights reserved.


Disclaimer     Privacy policy     Imprint

Spring Data JPA Course x

Price Increases In
03  D : 15  H : 33  M : 20  S
Sign Up Now!

https://thorben-janssen.com/can-you-use-hibernate-eclipselink-jpa-for-your-microservice/ 13/13

You might also like