A walk to Lazy Fetching With Hibernate and Spring Data JPA

Syed Hasan
3 min readSep 26, 2021

If you already have read my article about Handling MultipleBagFetchException in Hibernate while using FetchType.EAGER then probably you are ready to understand this problem and it’s troubleshooting.

Background

Using FetchType.EAGER is a very bad practice, since our services may not require all the data of the mapped entities in all cases. And moreover it is a bad idea to fetch so many data in a single session and making the session heavy. Therefore we will refactor the code again and use FetchType.LAZY.

Our old code for the entity was below:

After using FetchType.LAZY our new code will be like below:

But now when we try to build our project, we get a new problem. We have the LazyInitializationException.

LazyInitializationException

Troubleshooting LazyInitializationException

There might be found many suggestions over online but my preferred one (I found it really effective) is using something called EntityGraph.

A point to note, we can do it in 2 ways.

  1. One is using the annotations @NamedEntityGraphs, @NamedEntityGraph and @NamedAttributeNode annotations, which are provided by javax.persistence package.
  2. Using @EntityGraph annotation in repository interfaces. This annotation is provided by Spring Data JPA.

We will see both approaches here.

  1. Using javax.persistence provided annotations

We will annotate our above mentioned Status entity with the annotations shown below.

Explanation:

Here, @NamedEntityGraphs annotation accepts an array of @NamedEntityGraph. So we can define as many entity graphs as we need.

In the @NamedEntityGraph annotation we have defined a name which we will call later, and an attributes attribute which we will use mention which mapped entities we want to join for this named entity graph.

Notice that here we have only used the attributes owner and locations in our @NamedAttributeNode annotation, meaning that when we will call this named entity graph, the joined data will be fetched for these 2 attributes only and we will get no data for the attachments attribute.

Calling the named entity graph:

To call this named entity graph in our query, we have added the following additional lines (line 96, 97 and 101) in our existing code.

2. Using Spring Data JPA provided @EntityGraph annotation

Let’s see the entities first.

We have an entity which has a List of String Roles, mapped as @ElementCollection and it’s fetch type is FetchType.LAZY.

Now let’s see our UserRepository interface.

Here we have used the @EntityGraph annotation and provided the attribute name in attributePaths parameter. And it is that simple to do the whole stuff.

The code for javax.persistence provided annotations can be found in this github repository.

--

--

Syed Hasan

Software Engineer | Back-End Developer | Spring Developer | Cloud Enthusiast