Nisha Parveen
Nisha Parveen

Reputation: 13

Lazy and Eager fetching in hibernate

How lazy fetching works by default in hibernate using XML ?

Upvotes: 0

Views: 926

Answers (1)

codeLover
codeLover

Reputation: 2592

There are two modes of entity loading in hibernate lazy and eager. If you define entity configuration via XML mapping then lazy attribute is defined in the relationship tag(ie many-to-one, one-to-many etc.).

If you don't define lazy attribute then by default the lazy loading is enabled ie the value of lazy will be true but if you want eager loading then you need to turn off lazy loading by setting lazy=false.

Now if your lazy=true then select statement is executed whenever you try to access the child entities whereas if lazy=false (ie eager fetch is there), then you will notice that select query for child entities gets triggered immediately after the select query of parent entity. (You can notice this behavior by turning showSql to true in your console logs.)

Hope it clear outs all your queries

Upvotes: 2

Related Questions