
When working with JPA and Hibernate, developers often encounter the N+1 query problem.
This issue occurs when lazy-loaded associations cause excessive SQL queries, leading to performance bottlenecks.
Consider the following code:
@Entitypublic class Author {@OneToMany(mappedBy = "author", fetch = FetchType.LAZY)private List<Book> books;}@Servicepublic class AuthorService {public List<AuthorDto> getAllAuthorsWithBooks() {List<Author> authors = authorRepository.findAll(); // 1 queryreturn authors.stream().map(author -> new AuthorDto(author.getName(),author.getBooks().size() // N queries! One per author)).collect(Collectors.toList());}}
This is the N+1 query problem.
With JPQL fetch joins, you can load related entities in a single query.
@Repositorypublic interface AuthorRepository extends JpaRepository<Author, Long> {@Query("SELECT a FROM Author a LEFT JOIN FETCH a.books")List<Author> findAllWithBooks();}
Now, when you call findAllWithBooks(), Hibernate fetches both authors and their books in one optimized query.
@EntityGraph is a clean alternative that avoids writing custom JPQL.@Repositorypublic interface AuthorRepository extends JpaRepository<Author, Long> {@EntityGraph(attributePaths = {"books"})List<Author> findAll();}
This tells JPA to eagerly fetch books whenever findAll() is called, reducing the N+1 issue.
Sometimes you don’t need the full entity, only summary data. Instead of loading all books, fetch only what you need.
public interface AuthorBookCountProjection {String getName();Long getBookCount();}@Repositorypublic interface AuthorRepository extends JpaRepository<Author, Long> {@Query("SELECT a.name as name, COUNT(b) as bookCount " +"FROM Author a LEFT JOIN a.books b GROUP BY a.id, a.name")List<AuthorBookCountProjection> findAuthorsWithBookCount();}
This avoids fetching unnecessary data while still giving book counts per author.
The N+1 query problem is one of the most common performance pitfalls in JPA/Hibernate.
By applying fetch joins, @EntityGraph, or projections, you can optimize database access and ensure your application scales smoothly.
Thank you for reading our comprehensive guide on "Fixing the N+1 Query Problem in JPA with Fetch Joins and EntityGraph" We hope you found it insightful and valuable. If you have any questions, need further assistance, or are looking for expert support in developing and managing your projects. our team is here to help!
Reach out to us for Your Project Needs:
🌐 Website: https://www.prometheanz.com
📧 Email: [email protected]
Copyright © 2025 PrometheanTech. All Rights Reserved.