Bugs have recently been fixed in 
org.exolab.castor.persist.RelationCollection, but I am still seeing strange 
behavior when using lazy loading and trying to query database immediately 
after adding an object.

Sniplets below outlines the overall structure of my code.

Calling Address.getByClientId() causes following exception:

java.lang.RuntimeException: PersistenceException for lazy loaded object
org.exolab.castor.jdo.PersistenceException: Identities can't be null!

        at 
org.exolab.castor.persist.RelationCollection$IteratorImp.lazyLoad(RelationCollection.java:278)

        at 
org.exolab.castor.persist.RelationCollection$IteratorImp.next(RelationCollection.java:253)

        at java.util.ArrayList.addAll(ArrayList.java:423)

        at Address.getByClientId(Address.java:232)

        at test.TestAddress.test(TestAddress.java:36)

        at java.lang.reflect.Method.invoke(Native Method)

        at junit.framework.TestCase.runTest(TestCase.java:166)

        ...

Any suggestions on what is wrong is greatly appreciated. Please let me know 
if a full example is needed to investigate this further.

Kind Regards,
Jan Hansen

-------------------

Test:
...
Client c = Client.add("TEST Client for address","Description");
Address a = 
Address.add(c.getId(),"address1","address2","address3","city","zip","state","country");
Collection c = Address.getByClientId(c.getId());
...



Class Client:
...
public void addAddress(Address _address){
    _address.setClient(this);
    this.address.add(_address);
}
...


Class Address:
...
public static Collection getByClientId(int id){
    QueryResults results;
    ECCasterConnectionProvider con = new ECCasterConnectionProvider(); // 
Helper for instantiation
    Client c = null;
    Collection ret = new ArrayList();
    try
    {
        Database db = con.getClientConnectionDNK();  // Helper for instantiation
        db.begin();
        OQLQuery Oql = db.getOQLQuery("Select c from Client c where c.id="+id);
        results = Oql.execute();
        if(results.hasMore()){
            c = (Client)results.next();
            ret.addAll(c.getAddress());
        }
        db.commit();
        db.close();
    } catch (Exception e){
        e.printStackTrace();
    }
    return ret;
}

public static Address add(int clientId, String address1, String 
address2,String address3, String city, String zip, String state, String 
Country){
    Address address = null;
    try
    {
        Client c = null;
        OQLQuery Oql = null;
        QueryResults results = null;
        CasterConnectionProvider con = new ECCasterConnectionProvider(); // Helper 
for instantiation
        Database db = con.getClientConnectionDNK();  // Helper for instantiation
        address = new Address();

        address.setAddress1(address1);
        address.setAddress2(address2);
        address.setAddress3(address3);
        address.setCity(city);

        db.begin();
        Oql = db.getOQLQuery("Select c from Client c where c.id = " + clientId);
        results = Oql.execute();
        if(results.hasMore()){
            c = (Client)results.next();
            c.addAddress(address);
        }
        db.commit();
        db.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return address;
}
...

Mapping file:
<!--  Mapping for Client -->
<class name="Client" identity="id" key-generator="CLIENT_SEQ" >
    <description>Client</description>
    <map-to table="CLIENT" />
    <field name="id" type="integer" >
      <sql name="id" type="integer"/>
    </field>
    <field name="name" type="string" >
      <sql name="name" type="char"/>
    </field>
    <field name="description" type="string" >
      <sql name="description" type="char"/>
    </field>
    <field name ="address"
           type = "Address"
           required="false"
           lazy="true"
           collection="collection">
      <sql many-key = "clientId"/>
    </field>
</class>

<!--  Mapping for Address  -->
<class name="Address" identity="id" depends="Client" 
key-generator="ADDRESS_SEQ" >
    <description>ADDRESS</description>
    <map-to table="ADDRESS" />
    <field name="id" type="integer" >
      <sql name="id" type="integer"/>
    </field>
    <field name="address1" type="string" >
      <sql name="address1" type="char"/>
    </field>
    <field name="address2" type="string" >
      <sql name="address2" type="char"/>
    </field>
    <field name="address3" type="string" >
      <sql name="address3" type="char"/>
    </field>
    <field name="zip" type="string" >
      <sql name="zip" type="char"/>
    </field>
    <field name="city" type="string" >
      <sql name="city" type="char"/>
    </field>
    <field name="state" type="string" >
      <sql name="state" type="char"/>
    </field>
    <field name="country" type="string" >
      <sql name="country" type="char"/>
    </field>
    <field name="client" type="Client" >
      <sql name="clientid"/>
    </field>
</class>

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to