hibernate 3 uses lazy loading. But, is it true that lazy load is mainly used when our POJO are like
Class A { B b; }
Class B { .. }
i.e, POJO B is a class variable of POJO A, and meanwhile A and B map to two tables respectively ?
2. Without such class variable relation between the two POJOs, i.e. if we have several POJOs and each of them maps to a table, but none of them is a class variable of another POJO, then do we still need lazy load ??
Originally posted by Paul Sturrock: Lazy initialisation works for associated objects. If there is no association, then there can be no lazy loading.
Thank you. Couple more further questions
I) suppose we have
class A {
B b; .. }
class B {..}
A includes a B b; if we do NOT use lazy load, people talked about it may load the "entire database' (scary.) So for this case, does it may load the entire table of "B" or "A" ?
II) Sometimes the two tables have association (one holds a foreign key to the other). But since the key is not a surrogate key, in POJO classes we may just treat the FK attribute as a regular variable. If that's the case, do we need to use lazy load ?
for example
class A {
int class_B_id; // instead of using "B b;" .. }
class B { int id; // maps to the "class_B_id" .. }
A includes a B b; if we do NOT use lazy load, people talked about it may load the "entire database' (scary.) So for this case, does it may load the entire table of "B" or "A" ?
If you have an association between a table A and a table B that is not defined to use lazy initialization then when you load a record from A, you also load the corresponding related record(s) from B.
Sometimes the two tables have association (one holds a foreign key to the other). But since the key is not a surrogate key, in POJO classes we may just treat the FK attribute as a regular variable. If that's the case, do we need to use lazy load ?
Whether or not a foreign key is a surrogate key doesn't matter. You can treat the FK as a property rather than an associated object if you wish. I'm not sure what the advantage is though?