Professional Documents
Culture Documents
Hibernate, JPA
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]
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 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.
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.
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?
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:
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.
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?
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
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?
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?
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?
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?
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?
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