• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lazy loading versus eager fetch in hibernate

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the basically what eager and lazy loading mean..but when i was reading hibernate refernce document ..see below excerpt


Above it says it may trigger lazy load. I dont understand why will it lazy loading.

Also the document says

This time we did not use a fetch query to initialize the collection. Monitor the SQL log and try to
optimize this with an eager fetch.


How could we change the above to eager fetch ?
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define it on your relationship mapping annotation i,e



Be careful that this is what you really want though. Lazy loading is usually desirable. The example is merely stating that accessing the collection may trigger a lazy load. Sometime this is done purposefully and others times it may be unintentional hence the warning.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill..

I am new to Hibernate. Could you please let me know where should i exactly declare the annotation metnioned by you

@ManyToOne(fetch = FetchType.EAGER)
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try looking at some tutorials and see if that helps. Here is one to start you off.
http://www.mkyong.com/hibernate/hibernate-fetching-strategies-examples/
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is helpful....thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic