-->
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.  [ 2 posts ] 
Author Message
 Post subject: lazy loading - what does it do
PostPosted: Mon Feb 06, 2006 12:26 pm 
Newbie

Joined: Fri Jan 27, 2006 11:14 am
Posts: 12
Hi, im a hibernate starter. I get confused by the concept of lazy loading, here is my understanding, could you please correct errors or add other points if theres anything wrong.


First, If a class is defined as lazy, then when an object of that class is instantiated, its instance variables that reference other classes (like foreign keys link to other tables) are not instantiated, unless a method of that variable is explicitly called. For example,


Code:
public Class Booking{

    Customer cust;
    Car car;
    public Booking{}

    public Car getCar(){
        return car;
    }
....
}



If Booking.hbm.xml define "lazy=false", then when Booking obj instantiated, Hibernate will retrieve all info of Booking from booking_table, plus all info of cust from cust_table, plus all info of car from car_table;
while if "lazy = true," Hibernate will not instantiate cust or car, so Booking.getCar() will be null; however car.getId() will work fine and return the id of the car object. Am i right?


Second if a Set is defined lazy="true", hibernate will not load all elements of that set, but only a few.
For example

Code:
public Class Customer{
   Set bookings;
   public Customer()

   public Set getBookings(){
      return bookings;
   }
}


In above, if the mapping file for Customer.java specifies
<set ........ lazy="true">
..........
</set>
lets suppose for this Customer he has 100 bookings. but as lazy=true, getBookings() will not load all 100 but only a few of them. Am i right?


This is my understanding of lazy loading. thanks in advance for any corrections/explanations!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 12:43 pm 
Newbie

Joined: Mon Mar 07, 2005 7:25 am
Posts: 10
The easiest way to look at lazy loading is that Hibernate will refrain from running an extra SQL query until it's absolutely neccesary.

For instance, if you query object A and it has a "lazy" relationship with object B, it will not load any properties for object B until they are asked for (eg. a getter). However, object B will not be null if A has a key for it. It also explains why you can already load het B's key from the lazy loaded B object, even though the rest of B's fields might still not be there: when loading A, you already loaded this information.

Collections will not be loaded (queried) until you ask something of it (for instance the size). However, the size of the collection it fetches initially is controllable (look for batch-size properties). If you iterate over the collection, eventually everything will be loaded.

Note that lazy loading ceases to work when the associated connection is gone (eg. session.close()).

Experiment with my first statement in mind, and you'll figure it out.

T.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.