users@jersey.java.net

Re: [Jersey] JAXB and object relationships

From: Moises Lejter <moilejter_at_gmail.com>
Date: Tue, 6 Oct 2009 00:26:45 -0500

I don't believe that JAXB supports what you want, out of the box -
though I admit I could be wrong :-)

I can think of a relatively simple way to fake something workable,
using just features of JAXB - but it's not transparent:
- every JAXB class needs to have an extra attribute, "id", which
  could appear as an XML attribute, rather than element.
- on marshalling, set the id attribute of each object to a unique
  key, before marshalling each object.
- allow the marshall to proceed. The XML representation will
  have multiple copies of shared elements - but their id attribute
  would allow readers to tell.
- on unmarshalling, build a map of entities by id attribute, as
  they are unmarshalled.
- after unmarshalling each element, check whether any of its
  children have ids already in the table. if so, replace the child
  with the matching element already in the table.
- add all new elements to the table.

This can be implemented via Marshall.Listener (to ensure ids
are set before marshalling) and Unmarshall.Listener (to replace
children, if they are duplicates).
The id attributes could be set to hashCode(), if unique - else
a table could be built during marshalling, to assign unique ids
to each object being marshalled.

(I've never built something like this, so take it with a grain of salt...)

Moises

On Mon, Oct 5, 2009 at 11:53 PM, Steve Mactaggart <steve_at_whitesquaresoft.com
> wrote:

> We have been using Jersey for a project for the last few months and
> loving it. But this week have run into an issue that I think is with
> JAXB not Jersey.
> I'm posting the question here, as I guess others like me have probably
> run into the same issue and there might be a really simple answer.
>
> What I have is an element that has 2 children, each of them have
> children where some of their children reference each other.
>
> Let me show some code.
>
> Root root = new Root();
>
> Parent parent1 = new Parent();
> Parent parent2 = new Parent();
>
> root.add(parent1);
> root.add(parent2);
>
> Child child1 = new Child();
> Child child2 = new Child();
> Child child3 = new Child();
>
> parent1.add(child1);
> parent1.add(child2);
>
> parent2.add(child2);
> parent2.add(child3);
>
> So we have 1 root, 2 parents and 3 children.
>
> If I send this up and down the JAXB path, I seem to get back 4
> children. Each Parent has their own copy of child2. Is there anyway
> to get JAXB to serialise the relationship and show that both parent1
> and parent2 point to the same object?
>
> We only found this issue recently, when more complex elements were
> being transmitted.
>
> If there is no way to get JAXB to do this (thats what I believe at the
> moment), does anyone have any suggestions of a way I could do some
> magic in Jersey to re-instate the relationship?
>
> Cheers,
> Steve
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>