A java application I am working on has objects with a relationship similar to below. In the real application both objects are JPA entities.
class Underlying{}
class Thing
{
private Underlying underlying;
public Underlying getUnderlying()
{
return underlying;
}
public void setUnderlying(final Underlying underlying)
{
this.underlying = underlying;
}
}
There is a requirement in the application to create xml of the form:
<template>
<underlying>
<thing/>
<thing/>
<thing/>
</underlying>
</template>
So we have a situation where the object graph expresses the relationship between Thing and Underlying in the opposite direction to how it's expressed in the xml.
I expect to use JAXB to create the xml but ideally I don't want to have to create a new object hierarchy to reflect the associations in the xml. Is there any way to create xml of the form required from the entities in their current form (through the use of xml annotations or something)? I don't have any experience using JAXB but from the limited research I've done it doesn't seem like it's possible to reverse the direction of association in any straightforward way. Any help/advice would be greatly appreciated. One other option that has been suggested is to use XLST to transform the xml into the correct format. I have done no research on this topic as yet but I'll add to the question when I have some more info.
Thanks,
Matt.
The trick is making a instance of Underlying aware of the related instances of Thing. Below are a couple approaches you could leverate.
Option #1 - Make the Relationship Bidirectional
The easiest thing to do would be to make the relationship bidirectional. Then you could leverage the @XmlInverseReference extension in EclipseLink JAXB (MOXy). Note: I'm the MOXy tech lead:
Option #2 - Use an XmlAdapter
You could create an adapted version of Underlying. When building the adapted Underlying object the XmlAdapter could query the instances of Thing to populate the things property: