users@jaxb.java.net

_at_XmlElementWrapper and empty collections

From: Dmitri Colebatch <dim_at_colebatch.com>
Date: Fri, 7 Apr 2006 09:49:47 +1000

Hi all,

Firstly, the area that I'm asking about JAXB is definately doing the
right thing, however I'd like to have the option to make it do the
'wrong' thing. Let me explain.

We have classes that have hibernate and jaxb annotations on them. We
have xml that looks like this:

<foo>
  <bars>
    <bar> ... </bar>
    <bar> ... </bar>
  </bar>
  ...
</foo>

which is represented like this:

@XmlRootElement
@Entity
class Foo
{
  @XmlElementWrapper(name = "bars")
  @Xmlelement(name = "bar")
  Set<Bar> bars;
  ...
}

@XmlElement
@Entity
class Bar
{
  ...
}

When we have no bars, hibernate sets bars to be an empty set. JAXB
then sees an empty set (as opposed to null) and quite rightly spits
out this:

<foo>
  <bars />
  ...
</foo>

However I don't want the wrapper element if the collection is empty:

<foo>
  ...
</foo>

I realise that what JAXB is doing is correct, but would like to ask
for an option:

  @XmlElementWrapper(name="bars", wrapEmptyCollection=false)
  @XmlElement(name="bar")
  Set<Bar> bars;

Of course wrapEmptyCollection would default to true.

I realise this is a corner case and we're late in the piece, but is
there any chance of this being considered?

cheers
dim