2

我有以下层次结构:

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

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

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 模式时导致另一个问题,该模式现在将包含两个 ID 属性,这是不允许的。

难道我做错了什么?

4

1 回答 1

1

您看到的行为是一个错误(https://bugs.eclipse.org/353787)。我们已经检查了 EclipseLink 2.3.1 和 2.4.0 流的修复,这些流将从 2011 年 8 月 4 日开始在我们的夜间下载页面中提供:

于 2011-08-03T15:35:48.207 回答