moxy jaxb @XmlID和继承

时间:2011-08-03 14:56:30

标签: java inheritance jaxb moxy

我有以下层次结构:

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.

我希望Big类继承XmlID注释。我试过在Big类中添加“另一个”@XmlID注释,它修复了编组问题。但是,这会在生成XML Schema时导致另一个问题,现在它将包含两个ID属性,这是不允许的。

我做错了吗?

1 个答案:

答案 0 :(得分:1)

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