moxy jaxb @XmlID and inheritance



我有以下层次结构:

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
}

我试图编组类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;
    }
}

我使用以下测试代码:

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

不幸的是,在执行marshal时,我遇到了以下异常:

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.

我希望XmlID注释将由Big类继承。我曾尝试在Big类中添加"另一个"@XmlID注释,以修复封送问题。但是,在生成XML Schema时,这会导致另一个问题,它现在将包含两个ID属性,这是不允许的。

我做错了什么吗?

您所看到的行为是一个bug (https://bugs.eclipse.org/353787)。我们已经对EclipseLink 2.3.1和2.4.0流进行了修复,从2011年8月4日开始,可以从我们的夜间下载页面获得:

  • http://www.eclipse.org/eclipselink/downloads/nightly.php

最新更新