SlideShare a Scribd company logo
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)

More Related Content

PPTX
Domain Driven Design Quickly
PPTX
Domain Driven Design(DDD) Presentation
PPTX
The Apache Solr Semantic Knowledge Graph
PDF
Spring Framework - Core
PPTX
Spring framework
PDF
Spring Framework
PDF
Introduction to Spring Framework
Domain Driven Design Quickly
Domain Driven Design(DDD) Presentation
The Apache Solr Semantic Knowledge Graph
Spring Framework - Core
Spring framework
Spring Framework
Introduction to Spring Framework

What's hot (20)

PPTX
Static Analysis with Sonarlint
PPTX
React js
PPTX
Domain Driven Design 101
PPTX
React Architecture & Best Practices.pptx
PDF
openGauss DBMind.pdf
PPT
Code review
PPTX
What Is Virtual DOM In React JS.pptx
PPT
jpa-hibernate-presentation
PDF
Developing Faster with Swagger
PPTX
Domain-Driven Design
PPT
DDD Framework for Java: JdonFramework
PPTX
Domain Driven Design Introduction
PPTX
Code review
PDF
Entity Component Systems
PPTX
プログラマに優しいバグレポートの書き方
PPTX
Introduction to DDD
PDF
Domain Driven Design
PDF
역삼역, 이마트 AI_v최종.pdf
PPTX
Introduction to Sightly and Sling Models
DOC
τεχνολογίες διαδικτύου πανεπιστημιο πατρων
Static Analysis with Sonarlint
React js
Domain Driven Design 101
React Architecture & Best Practices.pptx
openGauss DBMind.pdf
Code review
What Is Virtual DOM In React JS.pptx
jpa-hibernate-presentation
Developing Faster with Swagger
Domain-Driven Design
DDD Framework for Java: JdonFramework
Domain Driven Design Introduction
Code review
Entity Component Systems
プログラマに優しいバグレポートの書き方
Introduction to DDD
Domain Driven Design
역삼역, 이마트 AI_v최종.pdf
Introduction to Sightly and Sling Models
τεχνολογίες διαδικτύου πανεπιστημιο πατρων
Ad

Similar to Tips about hibernate with spring data jpa (20)

PPTX
Running Neo4j in Production: Tips, Tricks and Optimizations
PPTX
Running Neo4j in Production: Tips, Tricks and Optimizations
PDF
Open Day July 2019
PPTX
DevOps, Performance Optimization and the Green Life with Magento
PPTX
Data weave 2.0 language fundamentals
PDF
Marco Liberati - Graph analytics
PDF
GraphQL ♥︎ GraphDB
PDF
How to review a pull request
PPTX
Simplifying training deep and serving learning models with big data in python...
PDF
Try the monad!
PDF
Numbers and Limits: Balancing Data and Design in Product Management
PDF
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
PDF
Modern Messaging for Distributed Systems
PDF
Measure everything you can
PDF
Getting big without getting fat, in perl
PPTX
Go/Ruby/Java: What's next?
PDF
Tetap Agile dengan Arsitektur Monolith - Ziya El Arief
PDF
Scalable, good, cheap
PDF
Don’t Bug Out! The Ins and Outs of Debugging FME Workflows
PDF
Strata 2016 - Lessons Learned from building real-life Machine Learning Systems
Running Neo4j in Production: Tips, Tricks and Optimizations
Running Neo4j in Production: Tips, Tricks and Optimizations
Open Day July 2019
DevOps, Performance Optimization and the Green Life with Magento
Data weave 2.0 language fundamentals
Marco Liberati - Graph analytics
GraphQL ♥︎ GraphDB
How to review a pull request
Simplifying training deep and serving learning models with big data in python...
Try the monad!
Numbers and Limits: Balancing Data and Design in Product Management
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
Modern Messaging for Distributed Systems
Measure everything you can
Getting big without getting fat, in perl
Go/Ruby/Java: What's next?
Tetap Agile dengan Arsitektur Monolith - Ziya El Arief
Scalable, good, cheap
Don’t Bug Out! The Ins and Outs of Debugging FME Workflows
Strata 2016 - Lessons Learned from building real-life Machine Learning Systems
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Spectroscopy.pptx food analysis technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Programs and apps: productivity, graphics, security and other tools
Spectroscopy.pptx food analysis technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
20250228 LYD VKU AI Blended-Learning.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25 Week I
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.

Tips about hibernate with spring data jpa