Eager vs Lazy Loading - Overview - Part 1

Chad Darby
A free video tutorial from Chad Darby
Popular Java Spring Instructor - Best Seller
4.6 instructor rating • 8 courses • 368,975 students

Learn more from the full course

Spring & Hibernate for Beginners (includes Spring Boot)

Spring 5: Learn Spring 5 Core, AOP, Spring MVC, Spring Security, Spring REST, Spring Boot 2, Thymeleaf, JPA & Hibernate

40:47:36 of on-demand video • Updated August 2020

  • Develop a REAL-TIME project with Spring MVC, Spring REST, Spring Boot and Hibernate CRUD ... all from SCRATCH
  • You will TYPE IN EVERY LINE of code with me in the videos. I EXPLAIN every line of code to help you learn!
  • LEARN key Spring 5 features: Core, Annotations, Java Config, AOP, Spring MVC, Hibernate and Maven
  • I am a RESPONSIVE INSTRUCTOR ... post your questions and I will RESPOND in 24 hours.
  • POPULAR VIDEOS for: Spring Boot 2, Spring Security, Spring REST, Spring Data JPA, Spring Data REST and Thymeleaf
  • Join an ACTIVE COMMUNITY of 140,000+ students that are already enrolled! Over 38,000+ Reviews - 5 STARS
  • Students have LANDED NEW JOBS with the skills from this course. Spring and Hibernate developers are in HIGH-DEMAND!
  • You can DOWNLOAD all videos, source code and PDFs. Perfect for offline LEARNING and REVIEW.
English -: In this video, we're gonna cover fetch types for eager versus lazy. Now, when we load data, or we retrieve or fetch data, the question is should we retrieve everything? So, an eager load will pull in everything. Lazy load will retrieve data only on request. So, imagine we have an instructor. An instructor has a list of courses. Depending on the loading type, that will determine when and how the data is loaded from the data base by Hibernate. And, it'll also has an implication on your actual performance of your given app. Now, with eager loading, it will actually load all your dependent entities. So, it'll load the instructor and and all of their courses at once. So, it will do a one, quick shot to the database, grab all the data and bring it back. Now, this may not be a big deal if you only have a small number instructor and a small number of courses, but you can imagine if you had a lot of data out there, that could actually impact the performance of your application. Now, let's take a look at another example with eager loading. So, what about a course and students? So, this could easily turn into a performance nightmare. So, if we were to load a course and then actually load all of the students for the course, then this could possibly slow down our application or not even possibly most likely, it will slow down our application. Because, just like the course you are attending now, a given course could have 10,000, 20,000, maybe 50,000 students and we really don't need all that data at this point. So, in our app, if we're simply searching for a course by keyword, like just doing your normal search there, we only want a list of matching courses. But eager loading, it will still load all the students for each course and that's not good, right? We only want the titles or the descriptions of the courses but not all of the students. So, the best practice ... ding, ding, ding! The best practice in the industry is to only load data when absolutely needed. So, you should prefer lazy loading instead of eager loading. Now, there's always exceptions to the rule, right? And there's also special use cases, but in general, the recommendation is to prefer lazy loading over eager loading. Alrighty, so with lazy loading. So, lazy loading will load the main entity first, and then it will actually load the dependent entities on demand, at a later time. So, here we have a course, so it will actually load the course first and then, when you need a list of students, then that's when it will actually go to the database and load those students on demand. So, they will be loaded at a later time. And, again, remember, the preference here is on making use of lazy loading to make sure our application performs in a fast manner. So, lazy loading is preferred.