What is Lazy Loading In Hibernate | Eklavya Online

What is Lazy Loading In Hibernate

Lazy setting decides whether to load child objects while loading the Parent Object.You need to do this setting respective hibernate mapping file of the parent class.lazy = true (means not to load child)By default the lazy loading of the child objects is true. This make sure that the child objects are not loaded unless they are explicitly invoked in the application by calling getChild()method on parent.In this case hibernate issues a fresh database call to load the child whengetChild() is actully called on the Parent object.But in some cases you do need to load the child objects when parent is loaded. Just make the lazy=false and hibernate will load the child when parent is loaded from the database.

Example: lazy=true (default)Address child of User class can be made lazy if it is not required frequently. lazy=false but you may need to load the Author object for Book parent whenever you deal with the book for online bookshop.

Lazy fetching means for example in hibernate if we use load() method then load() is lazy fetching i.e it is not going to touch the database until we write empobject.getString(eno); So when we write above statement in that instance it touch the database and get all the data from database. It is called as lazy loading.If we see another example i.e session.get(…) method is used then at that instance it is going to touch the database and get the data and place the data in session object it is called as eager loading.