Tips about
Hibernate with
Spring Data JPA
Thiago dos Santos Hora
Purpose of this presentation
Purpose of this presentation
● Alignment of knowledge
Purpose of this presentation
● Alignment of knowledge
● Discuss best practices
Purpose of this presentation
● Alignment of knowledge
● Discuss best practices
● Demonstrate problems and possible solutions
Purpose of this presentation
● Alignment of knowledge
● Discuss best practices
● Demonstrate problems and possible solutions
● Review the way we use JPA
A little bit of context
● JPA: Java Persistence API, Java specification responsible for defining a standard way to
work with ORM between different providers.
● Hibernate: Started in 2001, the most popular ORM Java Framework.
● Spring Data JPA: One of the best spring projects, abstracts all the data access layer
using interface and removing almost all boilerplate.
In other words, these tools help us to focus on business logic instead of writing the same
repetitive code over and over again, thus making the developers more business oriented.
Cool!!!! but...
Be careful
● As always, everything comes at a price.
● When we are dealing with a critical system where performance is paramount, we need
to pay closer attention to the way we access our data, in order to use strictly the bare-
minimum.
● These frameworks can help us, but, without the correct use of the tools that they
provide us with, we can end up having big performance problems.
So, let's look at some of these potential problems.
Our scenario
Our scenario
Our scenario
Our scenario
Starting with something simple:
Now, let's take a look at the generated query, just to make sure everything is fine...
Surprise!!!
N+1 Query Problem
● Relationship
● Performance
● Fetching plan
● Select statement
How to solve these problems?
FetchTypes
● Lazy
● Eager
Output
But sometimes you do need the data.
● Lazy loading data
Example
Problem
Output:
But sometimes you do need the data.
● Lazy loading data
● LazyInitializationException
Solution
Solution
But sometimes you do need the data.
● Lazy loading data
● LazyInitializationException
● Transactions
Solution
Moving forward, it's time to OneToOne
mapping
● Mapping Lazy One to One Relationship
Adding author contact information
Example
What?
Moving forward, it's time to OneToOne
mapping
● Mapping Lazy One to One Relationship
● Hibernate Issues
Hibernate one to one issue
Moving forward, it's time to OneToOne
mapping
● Mapping Lazy One to One Relationship
● Hibernate Issues
● Approches
Example
Not yet…
Moving forward, it's time to OneToOne
mapping
● Mapping Lazy One to One Relationship
● Hibernate Issues
● Approches
● Different alternatives to solve this
First, bytecode enhancement.
First, bytecode enhancement.
First, bytecode enhancement.
First, bytecode enhancement.
First, bytecode enhancement.
Running options
First, bytecode enhancement.
Running options
First, bytecode enhancement.
First, bytecode enhancement.
Second, hibernate bypass
Second, hibernate bypass
Second, hibernate bypass
Second, hibernate bypass
Third, avoid the bidirectional mapping
Third, avoid the bidirectional mapping
OK, but what if I need the all data?
● JPQL
● Join Fetch
Solution
Solution
In case you do not have defined the
FetchType as Lazy
OK, but what if I need the all data?
● JPQL
● Join Fetch
● Entity Graph
Example
Example
In case you do not have defined the
FetchType as Lazy
If you want to contribute with Hibernate
If you want to contribute with Hibernate
OK, but what if I need the all data?
● JPQL
● Join Fetch
● Entity Graph
● Named Entity Graph
Example
Output
In case you do not have defined the
FetchType as Lazy
That is not all about EntityGraphs.
That is not all about EntityGraphs.
That is not all about EntityGraphs.
Solution
Solution
What if you cannot update the Hibernate
version?
● Specifications
● Base Repository
● Extend spring repository functionalities
Solution
Solution
* must have the same name as the interface plus the suffix "Impl".
Solution
Solution
Solution
Let us talk about large amounts of data
● PagingAndSortingRepository
● Page Size
● Memory
Example
Example
Output
About the Batching of operations
● Batching
● Size
● Define properties
● Make sure your Hibernate provides batching support for your DB
Example
Example
Example
Example
Output
Project on Github
● https://github.com/thiagohora/tipsabouthibernatespringdatajpa (Demo project)
References
● https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ (Spring data jpa
documentation)
● https://hibernate.org/orm/documentation/5.4/ (Hibernate documentation)
● https://hibernate.atlassian.net/browse/HHH-9515 (One-to-one issue)
● https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-
hibernate/(Best way to map one-to-one)
● http://justonjava.blogspot.com/2010/09/lazy-one-to-one-and-one-to-many.html (one-to-one
inverse relationship)
● https://hibernate.atlassian.net/browse/HHH-8776 (Entity graphs handle no lazy attributes)
● https://www.baeldung.com/jpa-entity-graph (What the entity graph tries to resolve)
● https://blog.ippon.tech/boost-the-performance-of-your-spring-data-jpa-application/ (Boost the
performance of your Spring data jpa app)
● https://hibernate.atlassian.net/browse/HHH-10297 (Entity graph in clause issue)
● https://www.solidsyntax.be/2013/10/17/fetching-collections-hibernate/ (FetchMode)
References
● https://vladmihalcea.com/how-to-batch-insert-and-update-statements-with-hibernate/
● https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/
● https://www.baeldung.com/spring-data-jpa-batch-inserts
● https://vladmihalcea.com/how-to-customize-the-jdbc-batch-size-for-each-persistence-context-
with-hibernate/ (Batching operations)

Tips about hibernate with spring data jpa