This one time, at band camp, Werner Guttmann said:
WG>I am using Castor JDO 0.9.3.9, and i am just trying to get my head
WG>around lazy loading. I have a class Account that has two members of type
WG>AccountNature and AccountClassification. The relevant part of the
WG>mapping file looks as follows:
WG>
WG> <!-- account -->
WG> <class name="com.msdw.futopt.expiry.accounts.Account"
WG>identity="identifier">
WG> <cache-type type="time-limited" capacity="15" />
WG> <description>Represents an Account.</description>
WG> <map-to table="TLDR.OK_VW_LDR_ACCT" />
WG> <field name="identifier" type="string">
WG> <sql name="ACCT_ID" type="char"/>
WG> </field>
WG> <field name="description" type="string">
WG> <sql name="ACCT_NM" type="char"/>
WG> </field>
WG> <field name="companyCode" type="string">
WG> <sql name="CO_CD" type="char"/>
WG> </field>
WG> <field name="nature"
WG>type="com.msdw.futopt.expiry.accounts.AccountNature" lazy="true">
WG> <sql name="ACCT_NATR_ID" />
WG> </field>
WG> <field name="classification"
WG>type="com.msdw.futopt.expiry.accounts.AccountClassification"
WG>lazy="true">
WG> <sql name="ACCT_CLASS_ID" />
WG> </field>
WG> </class>
WG>
WG>Now, when executing the code in question, I get the following output on
WG>stdout
WG>
WG>Warning: Lazy loading of object is not yet support!
WG>
WG>Is this a problem of the version I am using, or have I missed something
WG>essential ?
Werner,
I believe that this exception occurs when you've related an object that
doesn't contain anything.
Just yesterday I asked Patrick van Kann to create some info for the JDO FAQ on
this topic. Here is his first draft:
----------------------------------------------------------------------------
LAZY LOADING RELATED QUESTIONS
How do I configure the JDO mapping to use the lazy loading feature?
Let us convert one of the JDO examples classes to use lazy-loading.
Information about the examples can be found here.
In the example model, one Product can contain a multitude of
ProductDetails. This is reflected in the conventional mapping as below.
Firstly, the mapping for Product.
<!-- Mapping for Product -->
<class name="myapp.Product"
identity="id">
<description>Product definition</description>
<map-to table="prod" xml="product" />
<field name="id" type="integer">
<sql name="id" type="integer" />
<xml name="id" node="attribute"/>
</field>
<!-- more fields ... -->
<!-- Product has reference to ProductDetail
many details per product -->
<field name="details" type="myapp.ProductDetail" required="true"
collection="vector">
<sql many-key="prod_id"/>
<xml name="detail" node="element" />
</field>
</class>
Now let us examine ProductDetail. Note, the relationship is mapped
bi-directionally as must be all relationships when using Castor JDO.
<!-- Mapping for Product Detail -->
<class name="myapp.ProductDetail" identity="id" depends="myapp.Product" >
<description>Product detail</description>
<map-to table="prod_detail" xml="detail" />
<field name="id" type="integer">
<sql name="id" type="integer"/>
<xml node="attribute"/>
</field>
<field name="product" type="myapp.Product">
<sql name="prod_id" />
<xml name="product" node="element" />
</field>
<!-- more fields ... -->
</class>
Let us now make the relationship between Product and ProductDetail use
lazy loading.
We need only change the way that the relationship to ProductDetail is
specified in the mapping of Product. The relevent field in Product can be
re-written
<field name="details" type="myapp.ProductDetail" required="true" lazy="true"
collection="collection">
<sql many-key="prod_id"/>
<xml name="detail" node="element" />
</field>
There have been 2 changes.
* We have placed the attribute lazy="true" in the field element
* We have changed the type of the underlying collection type to be a
Collection by changing the field element attribute to
collection="collection".
We are not finished yet! To complete the migration to lazy loading
requires a change in the Java class for Product. This is because the
underlying type in the persistent class now must be of type
RelationCollection. Read more about this in the question below.
Note that there is no change is required in the ProductDetail mapping.
I have modified my mapping to use lazy loading. Now I get a Field access
error. What am I doing wrong?
To use lazy loading you must also change the persistent class that holds
the collection of related objects. Specifically, the collection itself
must now be a special Castor-specific type of Collection called a
RelationCollection (org.exolab.castor.persist.RelationCollection). To see
the documentation for RelationCollection please click here.
RelationCollections only contain the identities of elements of one type.
This is the basis of lazy loading; rather than loading the entire "graph"
of related objects when the parent is loaded, just their primary keys are
loaded (a substantially less intensive operation for large graphs of
relations). If any element is needed, it will be fetched "on the fly".
For example, as you iterate through the RelationCollection, Castor
automatically fetches the full object and returns it.
To demonstrate how this is used, we will change the classes Product and
ProductDetail from the famous JDO examples to use lazy loading (and hence
the RelationCollection type). Read the question above for the low-down on
the mapping file changes needed.
In the original Product class we have the following code
import java.util.Vector;
...
private Vector _details = new Vector();
...
public Vector getDetails()
{
return _details;
}
public void addDetail( ProductDetail detail )
{
_details.add( detail );
detail.setProduct( this );
}
Note that the addDetail method automatically insures bi-directionality of
the relationship by calling setProduct(this) on each ProductDetail added.
Let us now make the necessary changes to set up lazy loading.
import org.exolab.castor.persist.RelationCollection;
...
private RelationCollection _details = new RelationCollection();
...
public Collection getDetails()
{
return _details;
}
public void addDetail( ProductDetail detail )
{
_details.add( detail );
detail.setProduct( this );
}
That's it. You are ready to use lazy loading.
----------------------------------------------------------------------------
Bruce
--
perl -e 'print unpack("u30","<0G)U8V4\@4VYY9&5R\"F9E<G)E=\$\!F<FEI+F-O;0\`\`");'
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev