0

I am currently reading about eager vs lazy loading. I am confused and comparing it to load vs no-load.

let's say i have two sections of UI.In one of them i want to show child data along with parent data and in second UI i am just showing parent data.Then i should write two different calls at database level,one is loading child data and another in loading only parent data.

How lazy/eager loading can helpful in this case because if i do lazy loading then it will consume more time in first case as it will query later rather than using joins etc. and if i use eager then it will consume time in case 2 as there is no need of child data.

Please let me correct if i am wrong and tell me some cases where eager/lazy loading can be helpful.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ankit Gupta
  • 2,529
  • 2
  • 15
  • 26

1 Answers1

2

I will show this by an example. The scenario is as such;

You are a parent who has a kid with a lot of toys. But the current issue is whenever you call him (we assume you have a boy), he comes to you with all his toys as well. Now this is an issue since you do not want him carrying around his toys all the time.

So being the rationale parent, you go right ahead and define the toys of the child as LAZY. Now whenever you call him, he just comes to you without his toys.

But you are faced with another issue. When the time comes for a family trip, you want him to bring along his toys because the kid will be bored with the trip otherwise. But since you strictly enforced LAZY on the child’s toy, you are unable to ask him to bring along the toys. This is where EAGER fetching comes into play.

Harshit
  • 153
  • 1
  • 11
  • i am clear about all this,my question is how to handle this case?If i talk about hibernate and write a single call for fetching child then should it be lazy or eager because both are good in one case but bad for another as i have explained in my question,or simply i should write two different hibernate calls. – Ankit Gupta Dec 02 '14 at 13:09
  • [link]http://dinukaroshan.blogspot.in/2012/08/lazyeager-loading-using-hibernate-by.html i am reffering you this link may be it will help you – Harshit Dec 02 '14 at 13:21
  • if i enable lazy load then in case of family trip there will be two separate db calls one for parent data then another for child data while in this case it will be better if i could fetch all data at once,this is my point so i think we should not use lazy/eager,should write two separate db calls for two separate cases. – Ankit Gupta Dec 03 '14 at 05:22
  • yes you can write two seprate db call not an issue. – Harshit Dec 04 '14 at 08:44
  • and it will be better than using lazy/eager in single db call.right? – Ankit Gupta Dec 04 '14 at 10:26