Let's say I have a few simple classes:
Code:
public class Foo {
   private Integer fooId;
   private Map bars; // Each Foo owns a bunch of Bars, keyed by barType

// accessors here
}

public class Bar {
private Integer barId;
private Foo foo; // reference to it's containing Foo
private Integer barType; // this is the key that Foos use to map to this bar
private String baz; // this has interesting data in it
// more accessors here
}



I'd like my XML to look something like this:
Code:
<foo id="123">
<bar bar-type="1">
<!-- would like this Bar's foo reference to point to containing foo-->
<baz>interesting data</baz>
</bar>
<!-- more bars here -->
</foo>



Notice that in my XML, I'd rather that the <bar> elements not have a reference to their <foo>, because they're already contained in it. Also note that the key (bar-type) and value information (baz) are both directly contained by the <bar> element.


What I'm wondering is, how do I map this kind of structure? The best I've come up with is:
Code:
<mapping>
<class name="Foo" identity="fooId">
<map-to xml="foo"/>
<field name="fooId" type="integer">
<bind-xml name="id" node="attribute"/>
</field>
<field name="bars" collection="map">
<bind-xml name="bar">
<class name="org.exolab.castor.mapping.MapItem">
<field name="key" type="integer">
<bind-xml name="bar-type" node="attribute"/>
</field>
<field name="value" type="Bar">
<bind-xml>
<class name="Bar">
<field name="foo" type="Foo">
<bind-xml node="attribute" reference="true"/>
</field>
<field name="barType" type="integer">
<bind-xml name="bar-type" node="attribute"/>
</field>
<field name="baz" type="string"/>
</class>
</bind-xml>
</field>
</class>
</bind-xml>
</field>
</class>
</mapping>



I have several problems with this mapping. Here's the XML it generates:

Code:
<foo id="123">
   <bar bar-type="456">
       <value bar-type="456" foo="123">
           <baz>interesting data</baz>
       </value>
   </bar>
   <!-- more bars here -->
</foo>


For one, how do I get rid of the <value> element, and have the <baz> contained directly inside the <bar>, but it seems castor wants every element to include an XML binding, even if that element is just a reference to a class. Notice the duplicate bar-type attribute, I'd like that to go away as well.


Two, how do I get rid of the foo="123" inside my (currently <value>, but hopefully <bar>) element? I'd like castor to map the Bar.foo reference to the Foo instance that maps to the surrounding <foo> element. Is there an XPath-like way to reference other nodes in the document? Currently I end up post-processing my object model after Castor unmarshals it to manage this.

Last, since on the Java side my ids (fooId and barId) are Integers, I'd like them to be null for new (unpersisted) instances and be optional on the XML side (i.e., if there's an id attribute, that instance has been saved). This presents a problem if I cannot solve the problem above.

I'll admit to being a Castor newbie, and would appreciate any help you have. I've checked the FAQs and this forum, but couldn't find an answer to my questions.

Thanks!

Reply via email to