我具有以下层次结构:
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属性,这是不允许的。
难道我做错了什么?
最佳答案
您看到的行为是一个错误(https://bugs.eclipse.org/353787)。我们已经检入了对EclipseLink 2.3.1和2.4.0流的修复,这些修复将在2011年8月4日开始的每晚下载页面上提供:
http://www.eclipse.org/eclipselink/downloads/nightly.php
关于java - moxy jaxb @XmlID和继承,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6928521/