I have a class (Bundle) that contains a reference to a 'parent' instance of Bundle. This is used to record which object the new Bundle was 'copied' from. This class is contained within a List<> in another class (Main), and itself contains a List<> of (different) child classes (Elements).
When saved to XML, the reference attribute contains the IDs from the Elements contained within the parent Bundle, NOT the parent Bundle's ID.
My question is how can I get Bundle.parent to appear as Bundle.id in the XML?
Code snippets follow, all classes are FIELD accessor'd.
public class Element {
@XmlID
@XmlAttribute
public String id;
}
public class Bundle extends AbstractList<Element> {
@XmlID
@XmlAttribute
public String id;
@XmlElementWrapper
@XmlElemet(name = "element")
@XmlIDREF
public List<Element> elements;
@XmlElement
public Bundle parent;
}
public class Main AbstractList<Bundle> {
@XmlAttribute
@XmlID
public String id;
@XmlElementWrapper
@XmlElement(name = "bundle")
public List<Bundle> bundles;
@XmlElementWrapper
@XmlElement(name = "masterBundle")
static final public List<Bundle> masterBundles;
}
final JAXBContext jc = JAXBContext.newInstance(Main.class);
The Bundle.parent field is usually from a Bundle in bundles to a Bundle in masterBundles. Here is an example output:
<masterBundles>
<bundle id="999594e2-043d-45ed-9ee3-34b11e6297fd" name="Bundle1">
<elements>
<element>4501a2e5-05af-4760-92be-29dacb2e2d70</element>
<element>449cec85-7eb7-4ba9-b03e-2b5708bee076</element>
</elements>
</bundle>
</masterBundles>
<bundles>
<bundle id="7e07ae32-2bcc-49db-be0b-f04ab53f75e0" name="New Bundle">
<elements>
<element>4501a2e5-05af-4760-92be-29dacb2e2d70</element>
<element>449cec85-7eb7-4ba9-b03e-2b5708bee076</element>
</elements>
<parent>4501a2e5-05af-4760-92be-29dacb2e2d70</parent>
<parent>449cec85-7eb7-4ba9-b03e-2b5708bee076</parent>
</bundle>
</bundles>
Update: I think I found the problem, JAXB is treating the parent element as a Collection (which it is) so is dumping all the members of the collection! Annoying.