Home  >  Forum  >  Hibernate
Post New Query

FetchType.EAGER and FetchType.LAZY in Hibernate Annotation



Asked on February 14, 2020
I have read the entire tutorial
https://www.concretepage.com/hibernate/fetch_hibernate_annotation

I think there is a misunderstanding . Just by changing the fetch type to EAGER and getting the states size you are not able to prove what you were describing in your post .

you said that "If we set FetchType.LAZY, then until we fetch the collection, the collection will not be loaded. If we set FetchType.EAGER, then collection will be loaded at the same time when the parent entity is loaded."

Instead of writing this statements
Country con= (Country)session.get(Country.class,new Integer(1));
System.out.println(con.getStates().size());

try to write this statement
List<Country> con = sess.createQuery("From Country").list();

debug the program and expand con object .
you will see that when fetch type is EAGER two sql queries run
(one to get Country and other to get States)
and when fetch type id LAZY by default only one sql query runs
(one to get country ) until and unless we try to check states
in debug mode another sql query wont run .

This is how you prove that .

"If we set FetchType.LAZY, then until we fetch the collection, the collection will not be loaded. If we set FetchType.EAGER, then collection will be loaded at the same time when the parent entity is loaded."





Write Answer








©2020 concretepage.com | Privacy Policy | Contact Us