First of all a small example. The class ReferencingEntity holds a reference to the abstract class AbstractEntity. There are two implementations fo this class:
@XmlRootElement
public abstract class AbstractEntity {
@XmlID
private String id;
}
@XmlRootElement
public class EntityImpl1 extends AbstractEntity {
}
@XmlRootElement
public class EntityImpl2 extends AbstractEntity {
}
@XmlRootElement
public class ReferencingEntity {
@XmlIDREF
private AbstractEntity entity;
}
There is no problem marshalling an instance of ReferencingEntity (except that the concrete type is not present in xml), but when trying to unmarshal the xml representation, the descriptor is missing to determine the concrete implementation.
Currently I’m using an XmlAdapter to set all non-id fields null, but it would be better to use @XmlID if possible. Any ideas?
UPDATE:
I’m using RESTEasy in JBoss 6.1.0.Final and the provider creates the context as follows:
ContextResolver<JAXBContextFinder> resolver = providers.getContextResolver(JAXBContextFinder.class, mediaType);
JAXBContextFinder finder = resolver.getContext(type);
if (finder == null)
{
if (reader) throw new JAXBUnmarshalException("Could not find JAXBContextFinder for media type: " + mediaType);
else throw new JAXBMarshalException("Could not find JAXBContextFinder for media type: " + mediaType);
}
JAXBContext context = finder.findCachedContext(type, mediaType, annotations);
Below is my initial answer to your question. I imagine it will evolve as I better understand your use case.
ABOUT
@XmlID/@XmlIDREFEvery instance referenced from a field/property annotated with
@XmlIDREFalso needs to be referenced via containment. I’ll use the class below in this example.REGARDING INHERITANCE
JAXB (JSR-222) implementations can’t automatically discover subclasses, so you will need to be sure that the
JAXBContextis aware of them. One way to accomplish this is to use the@XmlSeeAlsoannotation on the parent class to point at the child classes.DEMO CODE
Demo
input.xml
Output
FOR MORE INFORMATION