Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

What is lazy loading in Java? I don't understand the process. Can anybody help me to understand the process of lazy loading?

1 Answer

0 votes
by (46k points)
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().

Related questions

0 votes
1 answer
asked Jul 26, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
asked Oct 14, 2019 in Java by Anvi (10.2k points)

Browse Categories

...