users@jaxb.java.net

Re: JAXB Unmarshalling and XmlIDREF using different stores

From: Florian Thiel <flo.thiel+javanet_at_googlemail.com>
Date: Thu, 6 Dec 2007 17:32:12 -0500

On Dec 4, 2007 1:19 PM, Florian Thiel <flo.thiel+javanet_at_googlemail.com> wrote:
> On Nov 29, 2007 4:24 PM, Aleksei Valikov <valikov_at_gmx.net> wrote:
> >
> > Hi.
> >
> > > We're using JAXB 2.1 to marshal/unmarshal Java objects to an XML
> > > database and to different files. Some objects contain XmlIDREFs to
> > > objects stored in another file. On unmarshalling one of these objects
> > > the referencing attribute is set to "null" because JAXB cannot resolve
> > > the reference. Is there a way around this? We store IDs and fill the
> > > referenced objects manually as an ugly workaround. Do all the elements
> > > referencing each other have to be in the same document for JAXB to
> > > work? There does not seem to be a callback mechanism to resolve the
> > > references on misses.
> > >
> > > Does the non-goal "traversal of JavaBean object graphs" mean this will not work?
> >
> > You'll just need to provide a custom id resolver.
> >
> > http://weblogs.java.net/blog/kohsuke/archive/2005/08/pluggable_ididr.html
>
> Thanks a lot! That was exactly what I was looking for. Now I wrote an
> IDResolver for testing and my startDocument method doesn't get called
> (checked with debugger and debug strings). How could that be? Isn't
> startDocument guaranteed to be called when marshalling starts?

After playing around a bit I established, that unmarshaling in general
works for our situation (we fetch objects by id from another source in
the resolver). Everything works fine if I move the loading of the
external source to the resolver method. That's obviously an
impractical solution because it would reload the external source for
every resolve() call. Does anyone have any pointers of what to check
to find out why startDocument() does not get called? Is that part of
the "this interface is subject to change without prior notice?"
policy?

> FYI: We're using JAXB2.1 and this is the resolver setup code:
> ---
> JAXBContext ctx = JAXBContext.newInstance(RootLevel.class);
> Unmarshaller unmarshaller = ctx.createUnmarshaller();
> unmarshaller.setProperty(IDResolver.class.getName(), new LevelTypeResolver());
> Object object = unmarshaller.unmarshal(in);
> ---

Thanks again,
Florian