moxy jaxb @XmlID 和继承

发布于 2024-11-27 23:51:23 字数 2197 浏览 1 评论 0原文

我有以下层次结构:

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 类中添加“另一个”@XmlID 注释,这解决了编组问题。然而,这会在生成 XML 模式时导致另一个问题,该模式现在将包含两个 ID 属性,这是不允许的。

我做错了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名。 2024-12-04 23:51:23

您看到的行为是一个错误(https://bugs.eclipse.org/353787)。我们已签入 EclipseLink 2.3.1 和 2.4.0 流的修复程序,该修复程序将于 2011 年 8 月 4 日起从我们的夜间下载页面提供:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文