Java object graph -> xml when direction of object association needs to be reversed.

Posted by Sigmoidal on Stack Overflow See other posts from Stack Overflow or by Sigmoidal
Published on 2011-01-10T11:49:33Z Indexed on 2011/01/10 11:53 UTC
Read the original article Hit count: 252

Filed under:
|
|
|

An application I have been 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.

© Stack Overflow or respective owner

Related posts about Xml

Related posts about jpa