Difference Between EAGER loading and LAZY loading in hibernet?

Difference Between EAGER and LAZY

Okay, so we talked about the fact that FetchType.LAZY is the default fetch type for all Hibernate annotation relationships. We also talked about the fact that when you use the Lazy fetch type, Hibernate won’t load the relationships for that particular object instance. So then, what’s the difference between Eager and Lazy?

Fetch type Eager is essentially the opposite of Lazy, Eager will by default load ALL of the relationships related to a particular object loaded by Hibernate. This means that if you change the relationship to be this:


  1. import javax.persistence.FetchType;  
  2. //....  
  3. //....  
  4. //....  
  6. @OneToOne(fetch=FetchType.EAGER)  
  7. @JoinColumn(name="user_profile_id")  
  8. private Profile getUserProfile()  
  9. {  
  10.   return userProfile;  
  11. }  

Hibernate will now load the user profile into the user object by default. This means that if you access user.getUserProfile() it won’t be NULL (unless the joining table actually doesn’t contain a matching row by the “user_profile_id” join column).

So, the long story short here is:

FetchType.LAZY = Doesn’t load the relationships unless explicitly “asked for” via getter
FetchType.EAGER = Loads ALL relationships

asked Jul 1, 2015 in hibernate by rajesh
0 votes

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.