users@jaxb.java.net

Re: _at_XmlElementWrapper and empty collections

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 10 Apr 2006 17:59:45 -0700

What if you use the beforeUnmarshal hook to replace empty set by null?

Dmitri Colebatch wrote:
> 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?



-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com