You might want to look into how I've handled this type of issue in JiBX. There you can use a factory or post-set method that has access to the stack of objects being unmarshalled to set the "up" link. There's an example of this at http://jibx.sourceforge.net/tutorial/binding-extend.html (the Item instances link to the containing Order).


JiBX can also work with ID/IDREF, of course, but you'd need to have very contrived XML representations to make use of these for bidirectional links. You'd basically need to replace XML of this type:

<order>
..
<item id="4832">5</item>
<item id="1222">1</item>
<item id="8311">3</item>
<item id="83111">2</item>
</order>

with something like:

<order id="ord">
..
<item id="4832" order="ord">5</item>
<item id="1222" order="ord">1</item>
<item id="8311" order="ord">3</item>
<item id="83111" order="ord">2</item>
</order>

- Dennis

Keith Visco wrote:

Vahan,

Castor does support bidirectional relationships, but only via ID/IDREF.

So you need to specify reference="true" on the bind-xml element for
"order" field of class Item. You also need to have an identity field
specified for the Order class.

Please see the paragraph on "reference" attribute here:

http://castor.exolab.org/xml-mapping.html#3.5-The-%3Cbind-xml%3E-element

--Keith

Vahan Harput wrote:


Hi,

I would like to know whether Castor can marshal/unmarshal objects which are
connected through a bidirectional aggregation relationship. Consider these
two classes:

public class Order {

   java.util.Set items; // Items of these order

   public void setItems (Set items)
   {
       this.items = items;
   }

   public Set getItems()
   {
       return items;
   }
}

public class Item {

   Order order; // The order this item belongs to

   public void setOrder (Order order)
   {
       this.order = order;
   }

   public Order getOrder()
   {
       return order;
   }
}

My mapping file looks like this:

<mapping>
   <class name="Order" auto-complete="true">
       <field name="items" type="Item" collection="set">
       </field>
   </class>

   <class name="Item" auto-complete="true">
       <field name="order" type="Order">
       </field>
   </class>
</mapping>

The following test case fails with "Process exited with code 128":

Order ord = new Order();

Item item1 = new Item();
Item item2 = new Item();

Set items = new HashSet();

items.add(item1);
items.add(item2);

ord.setItems(items);

item1.setOrder(ord);
item2.setOrder(ord);

Mapping mapping = new Mapping();
mapping.loadMapping("castor-mapping.xml");

FileWriter writer = new FileWriter("order.xml");

Marshaller marshaller = new Marshaller(writer);
marshaller.setMapping(mapping);

marshaller.marshal(ord);

Any ideas what the problem is?

Regards,

Vahan Harput






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

Reply via email to