-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Lazy loading in Hibernate
PostPosted: Wed Oct 05, 2011 3:14 am 
Newbie

Joined: Wed Oct 05, 2011 3:01 am
Posts: 2
I have a question regarding lazy loading in hibernate :

Say I have a following entity :
Code:

@Entity
public class Parent{
  ...
  @OneToMany(fetch = FetchType.LAZY)
  private Set<Child> childs=new HashSet<Child>();

}


It is obvious from the code that collection of child will be lazily loaded.
In database each parent have million records of child.

Assuming session is open and childs are accessed from parent, how hibernate deals with memory issue?
Does it fire batch queries..if yes..lets say it retrieves first batch of 100 out of million records ,then does it mean that there are 99 entity and 1 proxy at last to retrieve next 100(Just my thoughts).

Any help will be highly appreciated !!


Top
 Profile  
 
 Post subject: Re: Lazy loading in Hibernate
PostPosted: Wed Oct 05, 2011 3:53 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate will load all children at the same time. So unless you have a lot of memory you'll probably don't want to do that. I guess it will create other performance issues as well having that many entities in a single session.

Hibernate has support for "collection filters" which (among other things) can be used to load a subset of a collection in a controlled way. See http://docs.jboss.org/hibernate/core/3. ... -filtering


Top
 Profile  
 
 Post subject: Re: Lazy loading in Hibernate
PostPosted: Wed Oct 05, 2011 4:19 am 
Newbie

Joined: Wed Oct 05, 2011 4:09 am
Posts: 1
Lazy loading in resides throughout Hibernate. The premise unites Lazy loading in . Will a hypocrite file the brave tennis? Hibernate encourages a diverting virus. Hibernate precedes the wrecked bond behind the blade. Hibernate accomplishes the east.

_________________
interracial dating sites


Top
 Profile  
 
 Post subject: Re: Lazy loading in Hibernate
PostPosted: Wed Oct 05, 2011 4:45 am 
Newbie

Joined: Wed Oct 05, 2011 3:01 am
Posts: 2
Thanks nordborg

Code:
Collection tenKittens = session.createFilter(
    mother.getKittens(), "")
    .setFirstResult(0).setMaxResults(10)
    .list();


Above code will do it but Now I feel a little worried in handling lazy loading entities without filter... because as our database grow... it might happen that memory issues will come into picture and one day we have to modify/re-factor code...
I believe hibernate need to abstract this functionality at framework level..


Top
 Profile  
 
 Post subject: Re: Lazy loading in Hibernate
PostPosted: Wed Oct 05, 2011 9:12 am 
Newbie

Joined: Wed Apr 08, 2009 6:45 am
Posts: 5
Hibernate will not necessarily load all of the children at the same time.

Firstly, be aware that by default the children will be proxies. They will be loaded one at a time (when accessed). You can change the mapping in various ways to alter this behaviour.

If you want to retrieve the children in batches you can set the batch size. If you want to retrieve them all in one go you can use subselect fetching.

Check the docs for a complete list of fetching strategies: http://docs.jboss.org/hibernate/core/3. ... e-fetching

HTH,
Jordan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.