-->
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
PostPosted: Wed Jun 16, 2010 2:04 am 
Newbie

Joined: Fri Dec 12, 2008 1:19 am
Posts: 2
Hi All,

I am new to hibernate. When I went through some tutorial i got the concept lazy loading.
So what I have understood about lazyloading is, in hibernate by default lazy loading is true. That means when parent class is loaded,it will not load the child class. Am i right? If it is right
then I have the following scenario.

I have one student class and set of phone numbers.

so When I load the studentclass from the session it is already loading the set phone numbers too..I am getting details of child class as well.

Following is the code:

Code:
Student student = (Student)session.load(Student.class, new Long(12));
      Set<Phone> phoneset=student.getStudentPhoneNumbers();
      System.out.println("phoneset"+phoneset.iterator().next().getPhoneNumber());
      System.out.println("studentid: "+student.getStudentId());
      System.out.println("student name: "+student.getStudentName());


Please make me correct if I am wrong.

Thanks
Bhanu lakshmi.


Top
 Profile  
 
 Post subject: Re: lazy loading
PostPosted: Wed Jun 16, 2010 5:57 pm 
Newbie

Joined: Fri Jun 04, 2010 10:54 am
Posts: 10
Location: USA
The call to student.getStudentPhoneNumbers() will load the set of phone numbers. The idea behind lazy loading is that it won't automatically load the associations unless they're asked for. In this case, you ask for the association right after loading the parent object, so that's when Hibernate will load the set. You may not have needed the set of phone numbers though in this session, and if you did not ask for them, Hibernate would not have loaded them.

HTH,
-Jeff


Top
 Profile  
 
 Post subject: Re: lazy loading
PostPosted: Thu Jun 17, 2010 2:52 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
The call to student.getStudentPhoneNumbers() will load the set of phone numbers


No, the phone number are not loaded in this call. It doesn't happen until the Set is used. In this case it is in the next line of code, phoneset.iterator() that loads the phone numbers.


Top
 Profile  
 
 Post subject: Re: lazy loading
PostPosted: Thu Jun 17, 2010 9:58 am 
Newbie

Joined: Fri Jun 04, 2010 10:54 am
Posts: 10
Location: USA
Thanks for setting me straight, nordborg. That's an important distinction.

Kind regards,
-Jeff


Top
 Profile  
 
 Post subject: Re: lazy loading
PostPosted: Thu Jul 15, 2010 6:28 am 
Newbie

Joined: Fri Dec 12, 2008 1:19 am
Posts: 2
Thanks all... I got the point... :)


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.