Member-only story
Java Performance Optimization Tips: Configuring the Default Batch Fetch Size Properly
Using Spring Data with Hibernate as the de facto ORM framework can be tempting. Its declarative behavior allows for rapid and effective development of Java POC applications. However, Hibernate’s default settings can introduce performance bottlenecks. I will discuss one optimization tip: batch_fetch_size.
1. Use case
Imagine a bookstore application where the requirement is to list all store authors and their books in a 2x2 Excel matrix format.
To make things possible, we already have book and author tables in the relational database structure.
- Each book has one author.
- Each author may have multiple books.
Our goal is to efficiently fetch all authors along with their books.
2. Hibernate entities
So the initial point is to create Hibernate entities to describe and model the database structure of 2 tables.
2.1 Author
Author is the main entry point where all the queries are made from. It works as a root entity for fetching data.
@Entity
public class Author {
@Id
@GeneratedValue
private Long authorId;
private String…