Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is lazy loading in Hibernate?

user-image
Question added by Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company
Date Posted: 2013/08/19
Usama Saad
by Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company

Lazy loading means that all associated entities and collections aren't initialized if you load an entity object.you have a parent and that parent has a collection of children. 
Hibernate now can "lazy-load" the children, which means that it does not actually load all the children when loading the parent. 
Instead, it loads them when requested to do so.
You can either request this explicitly or, and this is far more common, hibernate will load them automatically when you try to access a child. 
Lazy-loading can help improve the performance significantly since often you won't need the children and so they will not be loaded.
Also .
Hibernate will not actually load all children when you access the collection. 
Instead, it will load each child individually.
When iterating over the collection, this causes a query for every child. 
In order to avoid this, you can trick hibernate into loading all children simultaneously, e.g.
by calling parent.getChildren().size(). 
  This feature is enabled by default in Hibernate.
This feature can be set on the level of one-to-one or one-to-many relations.
For More Details : http://usama-saad.blogspot.com/2013/02/what-is-lazy-loading-in-hibernate.html

Mark Guirguis
by Mark Guirguis , IT Specialist , FAB Concepts

Say you have a parent and that parent has a collection of children.
Hibernate now can "lazy-load" the children, which means that it does not actually load all the children when loading the parent.
Instead, it loads them when requested to do so.
You can either request this explicitly or, and this is far more common, hibernate will load them automatically when you try to access a child.
Lazy-loading can help improve the performance significantly since often you won't need the children and so they will not be loaded.
Also beware of the n+1-problem.
Hibernate will not actually load all children when you access the collection.
Instead, it will load each child individually.
When iterating over the collection, this causes a query for every child.
In order to avoid this, you can trick hibernate into loading all children simultaneously, e.g.
by calling parent.getChildren().size().

Wasib Ali Khan
by Wasib Ali Khan , Software Engineer , Emaratech FZ LLC

The simplest way that Hibernate can apply lazy load behavior upon your entities and associations is by providing a proxy implementation of them. Hibernate intercepts calls to the entity by substituting a proxy for it derived from the entity’s class. Where the requested information is missing, it will be loaded from the database before control is ceded to the parent entity’s implementation.

ijaz saeed
by ijaz saeed , Lead Software Engineer , Macrosoft

An object that doesn't contain all of the data you need but knows how to get it.
There are four main varieties of lazy load. 
Lazy Initialization uses a special marker value (usually null) to indicate a field isn't loaded.
Every access to the field checks the field for the marker value and if unloaded, loads it. 
Virtual Proxy is an object with the same interface as the real object.
The first time one of its methods are called it loads the real object and then delegates. 
Value Holder is an object with a getValue method.
Clients call getValue to get the real object, the first call triggers the load.
Aghost is the real object without any data.
The first time you call a method the ghost loads the full data into its fields.

mirza habeeb
by mirza habeeb , Application Support & Database Developer , Via Technology

you have a parent and that parent has a collection of children.
Hibernate now can "lazy-load" the children, which means that it does not actually load all the children when loading the parent.
Instead, it loads them when requested to do so.
You can either request this explicitly or, and this is far more common, hibernate will load them automatically when you try to access a child.
Lazy-loading can help improve the performance significantly since often you won't need the children and so they will not be loaded.
Also beware of the n+1-problem.
Hibernate will not actually load all children when you access the collection.
Instead, it will load each child individually.
When iterating over the collection, this causes a query for every child.
In order to avoid this, you can trick hibernate into loading all children simultaneously, e.g.
by calling parent.getChildren().size().

Ajith Fernandez
by Ajith Fernandez , Software Engineer , Nayasoft Solutions Pvt.ltd

Lazy Loading means loading child objects while loading the parent object. Settings must be done in hibernate mapping XML/Parent file of the parent class.

If lazy=true means not to load child objects wile loading the parent objects.

If lazy=false means load the child object while loading the parent object.

Default true.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.