users@jaxb.java.net

Re: _at_XmlElementWrapper and empty collections

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 10 Apr 2006 18:28:30 -0700

Dmitri Colebatch wrote:
> Hi Kohsuke,
>
> On 4/11/06, Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_sun.com> wrote:
>>
>> What if you use the beforeUnmarshal hook to replace empty set by null?
>
> Yeah that's what we're doing atm, the problem is that we then need to
> be aware that the set may be null - a small thing I know, but if it is
> possible to avoid it I'd like to.

I see. The problem is, changing annotations for 2.0 is almost impossible
at this point. So I'm trying to think of something that you can use
without the spec change.

Oh, what if you define a private getter/setter just for JAXB?

@XmlRootElement
@Entity
class Foo
{
   @XmlTransient
   Set<Bar> bars;
   ...

   @XmlElementWrapper(name = "bars")
   @Xmlelement(name = "bar")
   private Set<Bar> getBars() {
     if(bars.isEmpty()) return null;
     else return bars;
   }
   private void setBars(Set<Bar> bars) {
     this.bars = bars; // or copy. either way it should work
   }
}


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com