So I've been reading up on using IDs and IDREFs in JAXB here, and in the example presented they make use of 1 IDREF element, which appears as an Object in the generated Java code. I include both the XML Schema...
<xsd:complexType name="CustomerType">
<xsd:sequence>
<xsd:element name="id" type="xsd:ID"/>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="OrderType">
<xsd:sequence>
<xsd:choice>
<xsd:element name="customer" type="CustomerType"/>
<xsd:element name="custref" type="xsd:IDREF"/>
</xsd:choice>
<xsd:element name="items" type="ItemType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
and the Java code for posterity.
public class OrderType {
protected CustomerType customer;
protected Object custref;
protected List items;
public CustomerType getCustomer() {
return customer;
}
public void setCustomer(CustomerType value) {
this.customer = value;
}
public Object getCustref() {
return custref;
}
public void setCustref(Object value) {
this.custref = value;
}
public List getItems() {
if (items == null) {
items = new ArrayList();
}
return this.items;
}
}
Now, in my code, I tried something a tad different, exemplified by the following schema:
<xsd:complexType name="SimViewType">
<xsd:sequence>
<xsd:element name="NAME" type="xsd:string"></xsd:element>
<xsd:element name="ITEM" type="am:SimItemIDREF" minOccurs="0" maxOccurs="unbounded"></xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"></xsd:attribute>
</xsd:complexType>
<xsd:simpleType name="SimItemIDREF">
<xsd:restriction base="xsd:IDREF"></xsd:restriction>
</xsd:simpleType>
Though I've enclosed a general IDREF with a SimItemIDREF, it's just for readability. However, even if I use a normal xsd:IDREF instead, the simple fact I use a value other than 1 means that my Java code ends up with the following...
public class SimViewType {
...
protected List<JAXBElement<Object>> item;
...
}
Instead of a List<Object>
, I get a List<JAXElement<Object>>
. Now, if I want to make a JAXElement, I can do that fine, but I cannot add it to my SimViewType as there is no conversion between JAXElement<SimItemType>
and JAXElement<Object>
. The idea that SimViews don't actually maintain control of SimItems is crucial in my design, so simply removing references is just not a good idea as it can create dangerous discrepancies at runtime.
How can I create a SimItemType or JAXBElement such that I can use it with my SimViewType?