简体   繁体   中英

moxy jaxb @XmlID and inheritance

I am having the following hierarchy:

public class Small {
    private String xmlId;

    @XmlID
    @XmlAttribute
    public String getXmlId() {
        if (xmlId == null)
            xmlId = "small" + new Random().nextInt();
        return xmlId;
    }

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

public class Big extends Small {
    // Code
}

Where I am trying to marshal the class Baz:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Baz {
    private List<Small> smalls = new LinkedList<Small>();

    private Small small;
    private Big big;

    @XmlIDREF
    public Small getSmall() {
        return small;
    }

    public void setSmall(Small small) {
        this.small = small;
    }

    @XmlIDREF
    public Big getBig() {
        return big;
    }

    public void setBig(Big big) {
        this.big = big;
    }

    @XmlElementWrapper(name = "smalls")
    @XmlElement(name = "small")
    public List<Small> getSmalls() {
        return smalls;
    }

    public void setSmalls(List<Small> smalls) {
        this.smalls = smalls;
    }
}

I am using the following test code:

public class Test2 {
    public static void main(String[] args) throws Exception {
        Small s1 = new Small();
        Small s2 = new Small();
        Big b1 = new Big();

        List<Small> smalls = new LinkedList<Small>();
        smalls.add(s1);
        smalls.add(s2);
        smalls.add(b1);

        Baz baz = new Baz();
        baz.setSmalls(smalls);
        baz.setSmall(s2);
        baz.setBig(b1);

        JAXBContext jc = JAXBContext.newInstance(Baz.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(baz, System.out);
    }
}

Unfortunately when doing the marshal, I am faced with the following exception:

Exception in thread "main" javax.xml.bind.JAXBException: 
Exception Description: Invalid XmlIDREF on property [big].  Class [test.moxy.Big] is required to have a property annotated with XmlID.

I would expect that the XmlID annotation would be inherited by the Big class. I have tried adding "another" @XmlID annotation in the Big class, which fixes the marshal problem. This however leads to another problem when generating a XML Schema, which will now contain two ID attributes, which is not allowed.

Am I doing something wrong?

The behaviour you are seeing is a bug ( https://bugs.eclipse.org/353787 ). We have checked in a fix to the EclipseLink 2.3.1 and 2.4.0 streams which will be available from our nightly download page starting August 4th, 2011:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM