Hibernate Fetch Type: Lazy or Eager? Simple Answer

Konstantin Triger
Javarevisited
Published in
4 min readSep 21, 2019

Table relationships (joins) are defined through foreign keys in database. JPA represents joins in the form of associations like One-to-One, One-to-Many, Many-to-One and Many-to-Many. Fetch Type decides on whether or not to load all the data that belongs to associations as soon as you fetch data from parent table. Fetch type supports two types of loading: Lazy and Eager. By default, Fetch type would be Lazy. When to use what?! In this article I will try to answer this question in a simple way.

Often I’m approached by a junior developer who asks this question. Usually I answer back by a question: “And what do you think?”. The common answer is something like: “I’m lost! I have several use cases, in some it should be Eager and in the others Lazy”. Well, definitely it cannot be both in the same time!

As I argue in Part 1 of this series, Hibernate is a great tool for CRUD. This is its primary design goal, and all primitives it has are built to support that goal. It means that in a case of a conflict the primary goal should win! So the decision regarding Fetch type should be taken for the CRUD context only.

To clear any confusions, CRUD context is the case when we allow editing a single root entity. And a root entity is something that usually has a business meaning. Users create, edit and delete them. Examples are products, orders, tasks or articles like this, that your read now.

And what should be done for the rest of the cases? This is what we need to think about. Not about Fetch type, since this question is already answered. We need to decide what data we need to retrieve from the database. The guide line is to retrieve just the data we really need.

How do we decide that? I will describe a decision tree I usually use.

  1. Is the data I retrieve is final, i.e. I return it more or less “as is” OR I intend to process it after retrieval? If the answer is “as is”, we probably need a DTO projection. I will elaborate on this option later.
  2. Let’s consider the option of processing data further. This is the point where I must answer the most complex question: How can I maximally reduce the dataset I retrieve from the database? And if I need to retrieve a dataset, that has a lot of referenced objects, I probably didn’t do a good job in reducing the returned dataset. Again, this question is complex because it’s not easy, and requires writing a more complex query. But if I do it, I’ll benefit in performance and maintainability. And finally, the result becomes another DTO projection, merging with the previous case.
  3. DTO projection or Tuple. This is how most of the non-plain CRUD queries should end. Why? Because tuples are specific to the use case! Remember the young developer? There are different cases where we want different behavior. We simply cannot use the same Entity in all of them! We need a new, tailored entity almost for every specific [advanced] case — DTO projection.

Summary

  • Use JPA Entities “as is” for CRUD and simple operations.
  • When there is a “fetch” conflict, the CRUD case should win. The “loosing” case should use a DTO projection.
  • Prefer delegating as much logic to Database as possible and return as few data as possible. If you retrieve data from the Database and do the in code calculation that could be done in the Database, it will always be significantly slower than doing it in the Database. You cannot compete with highly optimized data processing engine, with smart caches and indices. On that, you add time to retrieve data from the Database and related memory management.

And finally, since working with Tuples is not fully straight forward, FluentJPA library comes to rescue.

Other Hibernate Articles and Interview Questions you may like

  • Top 5 Courses to learn Hibernate and JPA in-depth (courses)
  • Difference between First and Second level cache in Hibernate? (answer)
  • Difference between get() and load() method in Hibernate? (answer)
  • 5 Spring and Hibernate Training Courses for Java developers (list)
  • 2 Books to Learn Hibernate in 2019 (books)
  • 5 Online Courses to learn Spring Boot in 2019 (courses)
  • 5 Books to Learn Spring Framework in 2019 (books)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • 10 Hibernate Questions from Java Interviews (list)
  • Top 15 Spring Boot Interview Questions for Java Programmers (questions)
  • Top 5 Courses to learn Spring Framework in-depth (courses)

--

--

Konstantin Triger
Javarevisited

I’m a Software Architect passionated about technology that solves real business problems