Use XmlIDRef for reference to abstract class

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);

Upvotes: 1

Views: 862

Answers (1)

bdoughan
bdoughan

Reputation: 148977

Below is my initial answer to your question. I imagine it will evolve as I better understand your use case.

ABOUT @XmlID/@XmlIDREF

Every instance referenced from a field/property annotated with @XmlIDREF also needs to be referenced via containment. I'll use the class below in this example.

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Root {

    private AbstractEntity abstractEntity;
    private ReferencingEntity referencingEntity;

    public AbstractEntity getAbstractEntity() {
        return abstractEntity;
    }

    public void setAbstractEntity(AbstractEntity abstractEntity) {
        this.abstractEntity = abstractEntity;
    }

    public ReferencingEntity getReferencingEntity() {
        return referencingEntity;
    }

    public void setReferencingEntity(ReferencingEntity referencingEntity) {
        this.referencingEntity = referencingEntity;
    }

}

REGARDING INHERITANCE

JAXB (JSR-222) implementations can't automatically discover subclasses, so you will need to be sure that the JAXBContext is aware of them. One way to accomplish this is to use the @XmlSeeAlso annotation on the parent class to point at the child classes.

import javax.xml.bind.annotation.*;

@XmlSeeAlso({EntityImpl1.class, EntityImpl2.class})
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractEntity {

    @XmlID
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

DEMO CODE

Demo

package forum12111815;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum12111815/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        System.out.println(root.getAbstractEntity().getClass());
        System.out.println(root.getAbstractEntity() == root.getReferencingEntity().getEntity());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <abstractEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="entityImpl2">
      <id>123</id>
   </abstractEntity>
   <referencingEntity>
      <entity>123</entity>
   </referencingEntity>
</root>

Output

class forum12111815.EntityImpl2
true
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <abstractEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="entityImpl2">
      <id>123</id>
   </abstractEntity>
   <referencingEntity>
      <entity>123</entity>
   </referencingEntity>
</root>

FOR MORE INFORMATION

Upvotes: 1

Related Questions