TL;DR: The code and annotations generated by xjc + jaxb2-annotate + xew does not produce the desired (or, for that matter, working out-of-the-box) xml structure, which should be a collection of IDREF elements.
If I manually change the jaxb attributes on the collection in the generated code, it works as I expect.
What is generated is:
@XmlElementWrapper(required = true) // from xew
@XmlElementRef(name = "investigator", namespace = "http://example/test", type = JAXBElement.class)
protected List<PersonType> investigators;
What I want is:
@XmlElementWrapper(required = true) // from xew
@XmlElement(name = "investigator", required = true, type = Object.class)
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected List<PersonType> investigators;
The generated annotations throw an exception at runtime saying PersonType does not have XmlRootElement. If I add that, it will run, but the XML collection is incorrect, having <person_type> elements instead of <investigfator> elements.
My question is how to get xjc to generate (what I consider to be the "correct") annotations?
Given the this schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example/test"
xmlns="http://example/test" elementFormDefault="qualified" >
<xs:element name="test_metadata" type="test_document_type"/>
<xs:complexType name="test_document_type">
<xs:sequence>
<xs:element name="investigators" >
<xs:complexType>
<xs:sequence>
<xs:element name="investigator" type="xs:IDREF" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="dataSubmitter" type="xs:IDREF" />
<xs:element name="people">
<xs:complexType>
<xs:sequence>
<xs:element name="person" type="person_type" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="person_type" id="ref_id">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="organization" type="xs:string" />
<xs:element name="contactInfo" type="xs:string" />
<xs:element name="identifier" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true" />
<xs:element name="link" type="xs:anyURI" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="ref_id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:schema>
and these xjc binding customizations:
<jaxb:bindings node="xs:complexType[@name='test_document_type']//xs:element[@name='investigators']//xs:element[@name='investigator']">
<jaxb:property>
<jaxb:baseType name="PersonType"/>
</jaxb:property>
</jaxb:bindings>
<jaxb:bindings node="xs:complexType[@name='test_document_type']//xs:element[@name='dataSubmitter']">
<jaxb:property>
<jaxb:baseType name="PersonType"/>
</jaxb:property>
</jaxb:bindings>
The (relevant) generated code is:
public class TestDocumentType implements Serializable
{
@XmlElementWrapper(required = true)
@XmlElementRef(name = "investigator", namespace = "http://example/test", type = JAXBElement.class)
protected List<PersonType> investigators;
@XmlElement(required = true, type = Object.class)
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected PersonType dataSubmitter;
@XmlElementWrapper(required = true)
@XmlElement(name = "person", namespace = "http://example/test")
protected List<xml.test.PersonType> people;
}
Notice the @XmlElementRef(name = "investigator", namespace = "http://example/test", type = JAXBElement.class)
When I use this generated code to build a Java object with a couple of <investigator> people and a <dataSubmitter> person (all also in the <people> collection), and then try serialize that using jaxb, the missing an @XmlRootElement annotation
exception mentioned above is thrown.
Now if I manually change the annotations on the
List<PersonType> investigators
field to
@XmlElement(name = "investigator", required = true, type = Object.class)
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected List<PersonType> investigators;
and run the code, I get the expected collection of IDREF elements:
<test_metadata xmlns="http://example/test">
<investigators>
<investigator>JohnHDoe</investigator>
<investigator>JackSmith</investigator>
</investigators>
<dataSubmitter>JohnHDoe</dataSubmitter>
<people>
<person ref_id="JohnHDoe">
<name>John H Doe</name>
<organization>semi</organization>
<contactInfo>tweedy</contactInfo>
</person>
<person ref_id="JackSmith">
<name>Jack Smith</name>
<organization>fully</organization>
<contactInfo>whistler</contactInfo>
</person>
</people>
</test_metadata>
Is there some control or binding customization that I need to correct jaxb annotations on the collection? Or another way to get the same XML structure?
Thanks