Aziz-

Just a quick reply while I'm in the middle of other things, you should
be able to model the relationships you want using identities and
references.  I recently wrote some documentation to that affect that
should be in the next build of Castor, or you can download the CVS
source and get it.

Short story, in the class element of the mapping file, you define an
identity field that uniquely identifies each object of that class. 
Then, when mapping the parentCity field in the street class, you
declare reference="true" in the bind-xml element, and Castor will keep
track of the relationship for you.

Sorry the explanation is a little rushed, but it is possible to model
these relationships.  Let me know if you have further questions.

Stephen


On 3/20/06, Aziz Hammadi <[EMAIL PROTECTED]> wrote:
> Hi All
> When I load a bean from an xml file using castor,
> the association between the child and parent elements in the xml file
> are not reflected in the object structure. Here is an example:
> XML:
> <country>
>   <city>
>     <street/>
>  </city>
> </country>
>
> OBJ:
> country.getYity(); // exists :-)
> city.getStreet();  // exists :-)
> street.getParentCity(); //<-- not possible :-(
>
> publi class Country{ private City city;    }
> public class City  { private Strret street;}
> public Street      {                       }
>
> As you can see, in the XML file, the street knows its parent because
> of the XML structure.
> As objects, a city knows its streets, but a street does not know its
> city directly.
>
> My idea to implement the missing association consists of the following steps:
> 1) add a property "parent" to each bean.
> 2) write an AssociationManager that sets the association child2parent
> directly after the objects have been unmarshalled from the xml file.
>
>
> Implementation approach:
> 1)
>    Source generation :
>     all castor generated beans inherit from a super class, that
> contains a property parent
>     public abstract class AbstractObject{
>         private Object parent;
>     }
>  2) Use in an application :
>      - unmarshall the xml file to an object tree that has the root country
>      - add the missing associations child2parent by calling the
> AssoiationManager
>         AssosiationManger.associate(country)
>
>     public class AssosiationManger{
>         public static assoicate(Object root){
>            // root is a Country object
>            // step 1: find fileds in Country.class using reflection
>            //            if the field inherits from AbstractObject
>            //                filed.setParent(root)
>            //                    associate(filed) // recursive call
>            //
>         }
>     }
>
> I would be very gald to hear feedback!
>
>
> Cheers
>
> Aziz Hammadi
>

Reply via email to