persistence@glassfish.java.net

RE: Need Doc help on Lazy Loading

From: Peter Krogh <peter.krogh_at_oracle.com>
Date: Tue, 12 Dec 2006 09:27:58 -0500

ToOnes (One to ones and Many to Ones) and ToManys (one to manys and many to manys) relationships work differently with respect to lazy loading.

> - in a One to Many mapping would company.getLocations() load the many
> side (Location) or is it expected to get loaded only on doing something
> with it eg: company.getLocations().iterator()?

in this example 'locations' would get loaded when you call something on the collection. This is because TopLink Essentials has its own Collection implementation that lazily loads relationships. In order for the relationship to be loaded, an action must be performed on TopLink Essentials Collection class.

> - Same question for Many To One, when will the relationship be loaded,
> on execution of employee.getDepartment() or
> employee.getDepartment().getName()?

Department would get loaded when you call getDepartment(). This is because TopLink Essentials weaves in a proxy class (ValueHolder) for one to one (and many to one) relationships. The method access is woven to trigger the proxy.

Peter

-----Original Message-----
From: Varun.Rupela_at_Sun.COM [mailto:Varun.Rupela_at_Sun.COM]On Behalf Of
Varun Rupela
Sent: Monday, December 11, 2006 11:47 PM
To: persistence_at_glassfish.dev.java.net
Subject: Need Doc help on Lazy Loading


Hi:

Kindly help with the below question. Pointers to docs that describe
toplink's lazy loading feature will be greatly appreciated.

When is a relationship loaded if the fetch attribute is set to LAZY- on
use of the getter or when doing an operation on the relationship entity
after using the getter. Example:
- in a One to Many mapping would company.getLocations() load the many
side (Location) or is it expected to get loaded only on doing something
with it eg: company.getLocations().iterator()?
- Same question for Many To One, when will the relationship be loaded,
on execution of employee.getDepartment() or
employee.getDepartment().getName()?

I found
http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html#LazyLoading
but it does not help with the above question.

thanks.

varun.