SlideShare a Scribd company logo
Building
Fast and Scalable
Persistence Layers
with
Spring Data JPA
Thorben Janssen
Independent consultant, trainer and author
@thjanssen123
/c/ThoughtsOnJava
thorbenjanssenofficial
Hibernate Tips
More than 70 solutions
to common Hibernate problems
www.hibernate-tips.com
Co-Organizer
JUG Paderborn
thorben-janssen.com
Fast & Scalable
 Find problems as early as possible
 Avoid / Fix performance problems
Performance
 Persistence provider: Hibernate
o Follow best practices
o Avoid pitfalls
Performance
Identify Issues
 Activate via system property
o hibernate.generate_statistics = true
 Configure logging
o org.hibernate.stat = DEBUG
Hibernate Statistics
 Configure threshold
o hibernate.session.events.log.LOG_QUERIES_SLO
WER_THAN_MS
Slow Query Log
Code Samples
Association Fetching
 Defines when the relationship will be fetched
 Static definition in entity mapping
FetchType
@ManyToOne(fetch = FetchType.LAZY)
private Publisher publisher;
FetchType
 Lazy
o Relationship gets loaded at first access
o Default for to-many relationships
 Eager
o Loads relationships immediately
o Default for to-one relationships
 To-many relationships
o Stick to the default mapping (FetchType.LAZY)
o Use eager fetching for specific queries, if required
 To-one relationships
o Check existing mappings individually
o Use FetchType.LAZY for new ones
Recommendation
Query-Specific Fetching
 Most common problems
o Lazy fetching of related entities
N+1 Select?
List<Author> authors = this.em.createQuery("SELECT a FROM Author a",
Author.class).getResultList();
for (Author a : authors) {
System.out.println("Author " + a.getFirstName() + " " + a.getLastName()
+ " wrote " + a.getBooks().size() + “ Books.”));
}
 Fetch all required entities with one query
o Fetch Joins
o EntityGraph
Query-Specific fetching
Code Samples
Many-to-Many Association
 Inefficient handling of List
o Remove all associations
o Add remaining ones
 Use Set instead
Many-to-Many
Code Samples
Projections
 Entities
 Scalar values
 DTO
Projections
 DTO classes
 DTO interfaces
o Spring Data JPA generates class
DTO Projections
Code Samples
 Nested associations
 Spring Expression Language
Advanced DTO Projections
Code Samples
Caching
Caching
Query
Cache
DB
2nd
Level
Cache
1st
Level
Cach
e
Hibernate
Session
1st
Level
Cach
e
Hibernate
Session
2nd Level Cache
 Session independent entity store
 Needs to be activated
o application.properties
 Transparent usage
 PersistenceProvider doesn‘t need to provide it
2nd Level Cache
 ALL all entities
 NONE no entities
 ENABLE_SELECTIVE requires activation
 DISABLE_SELECTIVE can be deactivated
 UNSPECIFIED use default settings
Shared Cache Mode
Code Samples
Spring Data JPA – Online Course
Learn Spring Data JPA in a structure way to
build efficient and maintainable persistence layers
https://thorben-janssen.com/course-spring-data-jpa/

More Related Content

PPTX
Introduction to ML (Machine Learning)
PDF
Artificial Intelligence Face recognition attendance system using MATLAB
PPTX
Text summarization using deep learning
PDF
The matplotlib Library
PDF
It all starts with the ' (SQL injection from attacker's point of view)
PDF
Project topics in Python with New Ideas
PDF
Smart attendance system using facial recognition
DOCX
Internship report
Introduction to ML (Machine Learning)
Artificial Intelligence Face recognition attendance system using MATLAB
Text summarization using deep learning
The matplotlib Library
It all starts with the ' (SQL injection from attacker's point of view)
Project topics in Python with New Ideas
Smart attendance system using facial recognition
Internship report

What's hot (20)

PDF
Student attendance management system
PDF
Spring Boot Tutorial | Microservices Spring Boot | Microservices Architecture...
PDF
Project final report
PPT
JMockit
PPTX
Developing High-Impact Malware with Minimal Effort.pptx
PDF
Pewarisan, Polimorfisme, dan Interface
PDF
Adversarial machine learning for av software
PDF
WannaCry Ransomware Attack: What to Do Now
PPTX
Detecting Fake News Through NLP
DOCX
Suspicious Activity Detection python Project Abstract
DOCX
Multiple object detection report
PDF
En route vers Java 21 - Javaday Paris 2023
PDF
Artificial Neural Networks Lect8: Neural networks for constrained optimization
PPTX
PPT Seminar TA Augmented Reality
PPTX
Project Super market billing system
PDF
MITRE AttACK framework it is time you took notice_v1.0
PPT
Traning and placement management system
PPTX
Learn Python The Hard Way Presentation
PPTX
Python Scipy Numpy
Student attendance management system
Spring Boot Tutorial | Microservices Spring Boot | Microservices Architecture...
Project final report
JMockit
Developing High-Impact Malware with Minimal Effort.pptx
Pewarisan, Polimorfisme, dan Interface
Adversarial machine learning for av software
WannaCry Ransomware Attack: What to Do Now
Detecting Fake News Through NLP
Suspicious Activity Detection python Project Abstract
Multiple object detection report
En route vers Java 21 - Javaday Paris 2023
Artificial Neural Networks Lect8: Neural networks for constrained optimization
PPT Seminar TA Augmented Reality
Project Super market billing system
MITRE AttACK framework it is time you took notice_v1.0
Traning and placement management system
Learn Python The Hard Way Presentation
Python Scipy Numpy
Ad

Similar to Building Fast and Scalable Persistence Layers with Spring Data JPA (20)

PPTX
Hibernate Performance Tuning @JUG Thüringen
PPTX
Effiziente persistierung
PDF
Effiziente Datenpersistierung mit JPA 2.1 und Hibernate
PPTX
Tips about hibernate with spring data jpa
PDF
JPA 2.1 performance tuning tips
PDF
ODP
Working with jpa
PDF
Java persistence api 2.1
PPT
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
PDF
Hibernate ORM: Tips, Tricks, and Performance Techniques
PDF
Spring Boot Tutorial Part 2 (JPA&Hibernate) .pdf
PPT
YDP_API&MS_UNIT_hiii detail notes to understand api.ppt
PPT
YDP_API&MS_UNIT_IIIii8iiiiiiiii8iiii.ppt
PPT
Accelerated data access
PDF
Second Level Cache in JPA Explained
PDF
Lazy vs. Eager Loading Strategies in JPA 2.1
PPTX
Spring Data JPA in detail with spring boot
PDF
Hibernate performance tuning
PPT
test for jpa spring boot persistence hehe
ODP
Polyglot persistence with Spring Data
Hibernate Performance Tuning @JUG Thüringen
Effiziente persistierung
Effiziente Datenpersistierung mit JPA 2.1 und Hibernate
Tips about hibernate with spring data jpa
JPA 2.1 performance tuning tips
Working with jpa
Java persistence api 2.1
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
Hibernate ORM: Tips, Tricks, and Performance Techniques
Spring Boot Tutorial Part 2 (JPA&Hibernate) .pdf
YDP_API&MS_UNIT_hiii detail notes to understand api.ppt
YDP_API&MS_UNIT_IIIii8iiiiiiiii8iiii.ppt
Accelerated data access
Second Level Cache in JPA Explained
Lazy vs. Eager Loading Strategies in JPA 2.1
Spring Data JPA in detail with spring boot
Hibernate performance tuning
test for jpa spring boot persistence hehe
Polyglot persistence with Spring Data
Ad

More from VMware Tanzu (20)

PDF
Spring into AI presented by Dan Vega 5/14
PDF
What AI Means For Your Product Strategy And What To Do About It
PDF
Make the Right Thing the Obvious Thing at Cardinal Health 2023
PPTX
Enhancing DevEx and Simplifying Operations at Scale
PDF
Spring Update | July 2023
PPTX
Platforms, Platform Engineering, & Platform as a Product
PPTX
Building Cloud Ready Apps
PDF
Spring Boot 3 And Beyond
PDF
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
PDF
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
PDF
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
PPTX
tanzu_developer_connect.pptx
PDF
Tanzu Virtual Developer Connect Workshop - French
PDF
Tanzu Developer Connect Workshop - English
PDF
Virtual Developer Connect Workshop - English
PDF
Tanzu Developer Connect - French
PDF
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
PDF
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
PDF
SpringOne Tour: The Influential Software Engineer
PDF
SpringOne Tour: Domain-Driven Design: Theory vs Practice
Spring into AI presented by Dan Vega 5/14
What AI Means For Your Product Strategy And What To Do About It
Make the Right Thing the Obvious Thing at Cardinal Health 2023
Enhancing DevEx and Simplifying Operations at Scale
Spring Update | July 2023
Platforms, Platform Engineering, & Platform as a Product
Building Cloud Ready Apps
Spring Boot 3 And Beyond
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
tanzu_developer_connect.pptx
Tanzu Virtual Developer Connect Workshop - French
Tanzu Developer Connect Workshop - English
Virtual Developer Connect Workshop - English
Tanzu Developer Connect - French
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
SpringOne Tour: The Influential Software Engineer
SpringOne Tour: Domain-Driven Design: Theory vs Practice

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
medical staffing services at VALiNTRY
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Nekopoi APK 2025 free lastest update
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
System and Network Administraation Chapter 3
PPTX
ai tools demonstartion for schools and inter college
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
top salesforce developer skills in 2025.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Odoo Companies in India – Driving Business Transformation.pdf
medical staffing services at VALiNTRY
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms II-SECS-1021-03
Nekopoi APK 2025 free lastest update
Computer Software and OS of computer science of grade 11.pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
System and Network Administraation Chapter 3
ai tools demonstartion for schools and inter college
Reimagine Home Health with the Power of Agentic AI​
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Operating system designcfffgfgggggggvggggggggg
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
CHAPTER 2 - PM Management and IT Context
top salesforce developer skills in 2025.pdf
Softaken Excel to vCard Converter Software.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

Building Fast and Scalable Persistence Layers with Spring Data JPA