EntityGraph is a powerful tool in the Java Persistence API (JPA) that allows us to optimize application performance by reducing the number of database accesses. By explicitly defining which entity relationships should be fetched, EntityGraph can prevent unnecessary joins and fetch only the required data. In this article, we will explore how EntityGraph works and how to use them effectively.
When an application needs to retrieve data from multiple related tables, by default JPA generates a separate SQL query for each relationship (which is usually called "lazy loading"), resulting in a large number of database queries.
Let’s consider two entities, Song and Tag, where one song can have many tags and one tag can be in many songs. Here there is an example implementation of these entities:
In this example, Song and Tag have a ManyToMany relationship.
Now, let's say we want to retrieve all songs and their corresponding tags. If we were to fetch all songs where on average each has 50 tags without using an EntityGraph, we would end up making, on average, 51 queries per song. This is known as the "N+1 queries problem", where a separate SQL query would be executed to retrieve each tag for each song.
To fix this issue using EntityGraph, we can define a "graph" that specifies that the tags should be fetched along with the Song entity, like in the example below.
We defined an EntityGraph that specifies that the tags should be fetched along with the Song in a single query, using the @NamedEntityGraph, which allows us to identify it by name, in this case Song.fullRead. Using the annotation @EntityGraph, we can pass as parameter the name of the EntityGraph we want to use.
It is possible and recommended to have multiple EntityGraph strategies at the same time, as in the example above, in order to optimize the amount of data fetched in each case.
To sum up, EntityGraph is a tool that can significantly improve application performance by reducing the number of database accesses. In the example included here, we managed to go from 51 queries on average to only 1, a massive improvement.