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!