Full Spring Boot Persistence Best Practices: Optimize Java Persistence Performance in Spring Boot Applications 1st Edition Anghel Leonard Ebook All Chapters
Full Spring Boot Persistence Best Practices: Optimize Java Persistence Performance in Spring Boot Applications 1st Edition Anghel Leonard Ebook All Chapters
com
https://textbookfull.com/product/pro-spring-security-securing-spring-
framework-5-and-boot-2-based-java-applications-2nd-edition-carlo-
scarioni/
textbookfull.com
https://textbookfull.com/product/the-bioarchaeology-of-metabolic-bone-
disease-2nd-edition-megan-brickley/
textbookfull.com
An Integrated Solution Based Irregular Driving Detection
1st Edition Rui Sun (Auth.)
https://textbookfull.com/product/an-integrated-solution-based-
irregular-driving-detection-1st-edition-rui-sun-auth/
textbookfull.com
https://textbookfull.com/product/intelligent-city-evaluation-system-
zhiqiang-wu/
textbookfull.com
https://textbookfull.com/product/exam-ref-70-761-querying-data-with-
transact-sql-1st-edition-safari/
textbookfull.com
https://textbookfull.com/product/the-american-military-a-narrative-
history-1st-edition-brad-d-lookingbill/
textbookfull.com
Responsible Corporate Governance Towards Sustainable and
Effective Governance Structures 1st Edition Maria Aluchna
https://textbookfull.com/product/responsible-corporate-governance-
towards-sustainable-and-effective-governance-structures-1st-edition-
maria-aluchna/
textbookfull.com
Spring Boot
Persistence
Best Practices
Optimize Java Persistence Performance
in Spring Boot Applications
—
Anghel Leonard
www.allitebooks.com
Spring Boot Persistence
Best Practices
Optimize Java Persistence
Performance in Spring Boot
Applications
Anghel Leonard
www.allitebooks.com
Spring Boot Persistence Best Practices: Optimize Java Persistence Performance in
Spring Boot Applications
Anghel Leonard
Banesti, Romania
www.allitebooks.com
This book is dedicated to my dear wife, Octavia.
www.allitebooks.com
Table of Contents
About the Author��������������������������������������������������������������������������������������������������xxiii
Chapter 1: Associations�������������������������������������������������������������������������������������������� 1
Item 1: How to Effectively Shape the @OneToMany Association�������������������������������������������������� 1
Always Cascade from Parent-Side to Child-Side�������������������������������������������������������������������� 2
Don’t Forget to Set mappedBy on the Parent-Side������������������������������������������������������������������ 2
Set orphanRemoval on the Parent-Side���������������������������������������������������������������������������������� 3
Keep Both Sides of the Association in Sync���������������������������������������������������������������������������� 3
Override equals( ) and hashCode( )����������������������������������������������������������������������������������������� 4
Use Lazy Fetching on Both Sides of the Association��������������������������������������������������������������� 5
Pay Attention to How toString( ) Is Overridden������������������������������������������������������������������������ 5
Use @JoinColumn to Specify the Join Column Name������������������������������������������������������������� 5
Author and Book Samples������������������������������������������������������������������������������������������������������� 6
Item 2: Why You Should Avoid the Unidirectional @OneToMany Association�������������������������������� 8
Regular Unidirectional @OneToMany������������������������������������������������������������������������������������ 10
Using @OrderColumn������������������������������������������������������������������������������������������������������������ 15
Using @JoinColumn�������������������������������������������������������������������������������������������������������������� 19
Item 3: How Efficient Is the Unidirectional @ManyToOne����������������������������������������������������������� 22
Adding a New Book to a Certain Author�������������������������������������������������������������������������������� 24
Fetching All Books of an Author��������������������������������������������������������������������������������������������� 26
Paging the Books of an Author���������������������������������������������������������������������������������������������� 27
Fetching All Books of an Author and Adding a New Book������������������������������������������������������ 28
Fetching all Books of an Author and Deleting a Book������������������������������������������������������������ 29
www.allitebooks.com
Table of Contents
vi
Table of Contents
Chapter 2: Entities�������������������������������������������������������������������������������������������������� 91
Item 13: How to Adopt a Fluent API Style in Entities������������������������������������������������������������������� 91
Fluent-Style via Entity Setters����������������������������������������������������������������������������������������������� 92
Fluent-Style via Additional Methods�������������������������������������������������������������������������������������� 95
Item 14: How to Populate a Child-Side Parent Association via a Hibernate-Specific Proxy�������� 98
Using findById( )�������������������������������������������������������������������������������������������������������������������� 99
Using getOne( )�������������������������������������������������������������������������������������������������������������������� 100
Item 15: How to Use Java 8 Optional in Persistence Layer������������������������������������������������������� 101
Optional in Entities�������������������������������������������������������������������������������������������������������������� 101
Optional in Repositories������������������������������������������������������������������������������������������������������ 103
Item 16: How to Write Immutable Entities��������������������������������������������������������������������������������� 104
Item 17: How to Clone Entities�������������������������������������������������������������������������������������������������� 106
Cloning the Parent and Associating the Books�������������������������������������������������������������������� 107
Cloning the Parent and the Books��������������������������������������������������������������������������������������� 110
Joining These Cases������������������������������������������������������������������������������������������������������������ 113
Item 18: Why and How to Activate Dirty Tracking���������������������������������������������������������������������� 114
Item 19: How to Map a Boolean to a Yes/No����������������������������������������������������������������������������� 117
Item 20: The Best Way to Publish Domain Events from Aggregate Roots��������������������������������� 119
Synchronous Execution������������������������������������������������������������������������������������������������������� 123
Asynchronous Execution����������������������������������������������������������������������������������������������������� 129
vii
Table of Contents
Item 29: Why to Pay Attention to Spring Projections that Include Associated Collections�������� 195
Using Nested Spring Closed Projection������������������������������������������������������������������������������� 195
Using a Simple Closed Projection���������������������������������������������������������������������������������������� 206
Transform List<Object[ ]> in DTO���������������������������������������������������������������������������������������� 208
Item 30: How to Fetch All Entity Attributes via Spring Projection���������������������������������������������� 213
Using the Query Builder Mechanism����������������������������������������������������������������������������������� 215
Using JPQL and @Query������������������������������������������������������������������������������������������������������ 215
Using JPQL with an Explicit List of Columns and @Query��������������������������������������������������� 217
Using a Native Query and @Query�������������������������������������������������������������������������������������� 218
Item 31: How to Fetch DTO via Constructor Expression������������������������������������������������������������ 219
Item 32: Why You Should Avoid Fetching Entities in DTO via the Constructor Expression�������� 223
Item 33: How to Fetch DTO via a JPA Tuple������������������������������������������������������������������������������� 226
Item 34: How to Fetch DTO via @SqlResultSetMapping and @NamedNativeQuery����������������� 229
Scalar Mappings������������������������������������������������������������������������������������������������������������������ 230
Constructor Mapping����������������������������������������������������������������������������������������������������������� 231
Entity Mapping��������������������������������������������������������������������������������������������������������������������� 233
Item 35: How to Fetch DTO via ResultTransformer������������������������������������������������������������������� 233
Item 36: How to Fetch DTO via a custom ResultTransformer���������������������������������������������������� 238
Item 37: How to Map an Entity to a Query via @Subselect������������������������������������������������������� 243
Item 38: How to Fetch DTO via Blaze-Persistence Entity Views������������������������������������������������ 247
Item 39: How to Effectively Fetch Parent and Association in One SELECT�������������������������������� 251
Item 40: How to Decide Between JOIN and JOIN FETCH����������������������������������������������������������� 256
Fetch All Authors and Their Books that Are More Expensive than the Given Price�������������� 257
How JOIN FETCH Will Act����������������������������������������������������������������������������������������������������� 258
How JOIN Will Act���������������������������������������������������������������������������������������������������������������� 259
Fetch All Books and their Authors��������������������������������������������������������������������������������������� 261
How JOIN FETCH Will Act����������������������������������������������������������������������������������������������������� 262
How JOIN Will Act���������������������������������������������������������������������������������������������������������������� 263
Item 41: How to Fetch All Left Entities�������������������������������������������������������������������������������������� 265
Item 42: How to Fetch DTO from Unrelated Entities������������������������������������������������������������������ 267
ix
Table of Contents
x
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Table of Contents
xi
Table of Contents
xii
Table of Contents
Item 68: How to Correctly Override equals( ) and hashCode( )������������������������������������������������� 448
Building the Unit Test����������������������������������������������������������������������������������������������������������� 448
Best Approaches for Overriding equals( ) and hashCode( )������������������������������������������������� 451
Approaches for Overriding equals( ) and hashCode( ) that Must Be Avoided���������������������� 458
Item 69: How to Use Hibernate-Specific @NaturalId in Spring Style���������������������������������������� 463
Testing Time������������������������������������������������������������������������������������������������������������������������ 467
Compound Natural ID���������������������������������������������������������������������������������������������������������� 468
Item 70: How to Use Hibernate-Specific @NaturalId and Skip the Entity
Identifier Retrieval��������������������������������������������������������������������������������������������������������������������� 471
Using @NaturalIdCache Solely�������������������������������������������������������������������������������������������� 472
Using @NaturalIdCache and @Cache���������������������������������������������������������������������������������� 474
Item 71: How to Define an Association that References a @NaturalId Column������������������������ 477
Testing Time������������������������������������������������������������������������������������������������������������������������ 478
Item 72: How to Obtain Auto-Generated Keys��������������������������������������������������������������������������� 479
Retrieve Auto-Generated Keys via getId( )��������������������������������������������������������������������������� 480
Retrieve Auto-Generated Keys via JdbcTemplate���������������������������������������������������������������� 480
Retrieve Auto-Generated Keys via SimpleJdbcInsert���������������������������������������������������������� 482
Item 73: How to Generate Custom Sequence IDs��������������������������������������������������������������������� 482
Item 74: How to Efficiently Implement a Composite Primary Key��������������������������������������������� 484
Composite key via @Embeddable and @EmbeddedId�������������������������������������������������������� 486
Composite key via @IdClass����������������������������������������������������������������������������������������������� 497
How About the Universally Unique Identifier (UUID)?���������������������������������������������������������� 498
Generate UUID via GenerationType.AUTO����������������������������������������������������������������������������� 499
Manually Assigned UUID������������������������������������������������������������������������������������������������������ 501
Hibernate-Specific uuid2����������������������������������������������������������������������������������������������������� 502
Item 75: How to Define a Relationship in a Composite Key������������������������������������������������������ 503
Testing Time������������������������������������������������������������������������������������������������������������������������ 508
Persist a Publisher��������������������������������������������������������������������������������������������������������������� 508
Persist Two Authors������������������������������������������������������������������������������������������������������������� 509
Find an Author by Name������������������������������������������������������������������������������������������������������ 512
xiii
Table of Contents
xv
Table of Contents
xvi
Table of Contents
xvii
Table of Contents
Item 121: How to Implement Advanced Search via Specification API��������������������������������������� 757
Testing Time������������������������������������������������������������������������������������������������������������������������ 761
What’s Next������������������������������������������������������������������������������������������������������������������������� 763
Item 122: How to Enhance SQL Statement Caching via IN Clause Parameter Padding������������ 763
Item 123: How to Create Specification Query Fetch Joins�������������������������������������������������������� 767
Join Fetch and Pagination in Memory��������������������������������������������������������������������������������� 767
Join Fetch and Pagination in Database������������������������������������������������������������������������������� 769
Item 124: How to Use a Hibernate-Specific Query Plan Cache������������������������������������������������� 772
Item 125: How to Check if a Transient Entity Exists in the Database via Spring Query
By Example (QBE)���������������������������������������������������������������������������������������������������������������������� 774
Head-to-Head Comparison of All Attributes������������������������������������������������������������������������� 776
Head-to-Head Comparison of Certain Attributes����������������������������������������������������������������� 777
Apply the or Conjunction to a Subset of Attributes�������������������������������������������������������������� 778
Item 126: How to Include in the UPDATE Statement Only the Modified Columns via
Hibernate @DynamicUpdate����������������������������������������������������������������������������������������������������� 779
Item 127: How to Use Named (Native) Queries in Spring���������������������������������������������������������� 781
Referencing a Named (Native) Query���������������������������������������������������������������������������������� 782
Using @NamedQuery and @NamedNativeQuery���������������������������������������������������������������� 782
Using a Properties File ( jpa-named-queries.properties)����������������������������������������������������� 784
Item 128: The Best Way to Fetch Parent and Children in Different Queries/Requests�������������� 787
Item 129: How to Optimize the Merge Operation Using Update������������������������������������������������ 791
Item 130: How to Implement Concurrent Table Based Queues via the SKIP LOCKED Option���� 794
Set Up SKIP LOCKED������������������������������������������������������������������������������������������������������������ 795
Testing Time������������������������������������������������������������������������������������������������������������������������ 796
Item 131: How to Retry the Transaction After a Versioned (@Version)
OptimisticLockException����������������������������������������������������������������������������������������������������������� 798
Versioned Optimistic Locking Exception������������������������������������������������������������������������������ 799
Simulate an Optimistic Locking Exception�������������������������������������������������������������������������� 801
Retrying the Transaction������������������������������������������������������������������������������������������������������ 802
Testing Scenario������������������������������������������������������������������������������������������������������������������ 806
xviii
Table of Contents
Appendix C: Five SQL Performance Tips That Will Save Your Day������������������������ 937
Using SQL Functions in the WHERE Clause������������������������������������������������������������������������������� 937
The Index Column Order Matters���������������������������������������������������������������������������������������������� 938
Primary Key vs. Unique Key������������������������������������������������������������������������������������������������������ 939
LIKE vs. Equals (=)�������������������������������������������������������������������������������������������������������������������� 939
UNION vs. UNION ALL and JOIN Flavors������������������������������������������������������������������������������������ 941
xxi
Table of Contents
Index������������������������������������������������������������������������������������������������������������������� 1009
xxii
About the Author
Anghel Leonard is a Chief Technology Strategist and
independent consultant with 20+ years of experience in the
Java ecosystem. In his daily work, he focuses on architecting
and developing Java distributed applications that empower
robust architectures, clean code, and high-performance.
Anghel is also passionate about coaching, mentoring, and
technical leadership. He is the author of several books,
videos, and dozens of articles related to Java technologies.
xxiii
About the Technical Reviewer
Manuel Jordan Elera is an autodidactic developer and
researcher who enjoys learning new technologies for his
own experiments and creating new integrations. Manuel
won the Springy Award – Community Champion and Spring
Champion 2013. In his little free time, he reads the Bible
and composes music on his guitar. Manuel is known as
dr_pompeii. He has tech reviewed numerous books for
Apress, including Pro Spring Boot 2 (2019), Rapid Java
Persistence and Microservices (2019), Java Language
Features (2018), Spring Boot 2 Recipes (2018), and Java APIs,
Extensions and Libraries (2018). Read his 13 detailed tutorials about many Spring
technologies, contact him through his blog at http://www.manueljordanelera.
blogspot.com, and follow him on his Twitter account, @dr_pompeii.
xxv
Introduction
In a nutshell, this book is a collection of best practices for Java persistence performance
in Spring Boot applications. These practices have been exposed via 120+ items, and they
can be grouped into three categories:
• Third, we deep dive into several Hibernate goodies that can sustain
the performance of your applications. By default, Spring Data relies
on Hibernate as its persistence provider, therefore you can exploit
Hibernate via Spring Data, and you can exploit underlying Hibernate
goodies as well. Goodies such as populating a child-side parent
association via a Hibernate proxy, using Dirty Tracking, delaying
connection acquisition, lazy loading attributes, and using natural
keys are just a few of the covered items.
The prerequisites of this book are pretty intuitive. You’ll need an IDE (e.g., NetBeans,
Eclipse, IntelliJ IDEA, Visual Studio, etc.), MySQL, and PostgreSQL. Optionally, you may
install or use other database vendors as well (e.g., Oracle, SQL Server, etc.).
xxvii
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of The Passing
of Morocco
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: English
A SAINT HOUSE.
THE
PASSING OF MOROCCO
BY
FREDERICK MOORE
To face page
Tangier Through the Kasbah Gate
10
A Riff Tribesman
A Maghzen Soldier } ” 96