users@jaxb.java.net

Complex references

From: Jeremy Haile <jhaile_at_fastmail.fm>
Date: Fri, 08 Jul 2005 10:12:14 -0400

I'm currently using Castor for my XML marshalling/unmarshalling, but
would like to transition to JAXB 2.0 using annotations because we have
had many problems with Castor.

We use XML marshalling/unmarshalling to load a large number of XML
configuration files into Java objects. We then persist these Java
configuration objects to the database using Hibernate.

One major problem we have encountered is that when unmarshalling and
using XML IDs, Castor does not distinguish between different Java class
types.

For example, with the following classes (using pseudo-JAXB 2.0
annotations, although we really use Castor mappings):

public class BasketOfFruit {
    
    @XmlIDRef Apple myApple;

    @XmlIDRef Orange myOrange;
}

public class Apple {
    @XmlID String name;
}

public class Orange {
    @XmlID String name;
}

When you unmarshal the following XML file, Castor would sometimes try to
set the Apple field with the Orange or visa-versa, since it only looks
at the XML ID:

<myFruits>
   <basketOfFruit apple="myFruit" orange="myFruit" />
   <apple name="myFruit"/>
   <orange name="myFruit"/>
</myFruits>

This is just an example - our XML has a large set of complex data,
including users, roles, workstations, domain configuration, etc. In
many cases, this data is entered by a user. We export the configuration
from our live system into XML files and later may wish to re-import this
data. In the example above - if users were allowed to create apples and
oranges and the system and export to XML, it wouldn't make sense for us
to prevent the user from naming an apple and an orange the same name -
and the marshalling to XML works fine. But when we go to import the
XML back into our system, Castor is confused as to whether a "myFruit"
XML ID refers to the apple or the orange.

So - now to my questions:
1) Has anyone else experienced these types of problems? Any ideas or
solutions?
2) Is JAXB 2.0 smarter than Castor in this regard?
3) Does JAXB 2.0 provide a mechanism by which to deal with these types
of problems.

If I wasn't clear enough or more explanation is needed - let me know.

Thanks,
Jeremy Haile