title: Elements annotated with @XmlElementRef not showing up in JAXB Marshalled Strings?
body: <p><strong>The Background</strong></p>
  <p>I am working with an application that uses data structures that we have retrieved and generated using jaxws-maven-plugin to communicate with an external application. We are able to receive the XML documents and unmarshall them into java objects using JAXB and the class structures they have provided us. However, when we re-marshall these objects into Strings to persist in the database for future debugging, we lose information.</p>
  <p>A snippet of the objects we use to convert:</p>
  <pre><code>/*the &quot;root&quot; object*/
  //XML tags indicating field access, name, and prop order
  public class PersonAppResObj {
      @XmlElement(name = &quot;ApplicationObj&quot;, required = true)
      protected ApplicationObj application;
      @XmlElement(name = &quot;ContactInfo&quot;, required = true)
      protected List&lt;ContactInfo&gt; contactInfo;
      ...
  }
  </code></pre>
  <pre><code>//XML tags indicating field access, name, and prop order
  public class ApplicationObj extends other.pkg.ApplicationObj {
      @XmlElement(name = &quot;PrimaryApplicant&quot;)
      protected List&lt;XObj&gt; primaryApplicant;
      ...
  }
  </code></pre>
  <pre><code>package other.pkg
  //XML tags indicating field access, name, and prop order
  public class ApplicationObj extends GenericApp {
      @XmlElementRef(name=&quot;Applicant&quot;, namespace=&quot;http://pkg.com/1/pkgs&quot;, type=JAXBElement.class, required=false)
      protected List&lt;JAXBElement&lt;? extends Applicant&gt;&gt; applicant;
      @XmlElement(name = &quot;ApplicationPrimaryContact&quot;, required = true)
      protected XObj applicationPrimaryContact;
      ...
  }
  </code></pre>
  <p>The following snippets are a simple Kotlin object that converts between the JAXB elements and the Strings, and a test for that object:</p>
  <pre><code>object Converter {
      fun xmlToXObj(respString: String): XObj? {
          val jaxbContext = JAXBContext.newInstance(ResponseRoot().javaClass)
          val unmarshaller = jaxbContext.createUnmarshaller()
          unmarshaller.setEventHandler {
              println(it.message)
              true
          }
          return (unmarshaller.unmarshal(ByteArrayInputStream(respString.toByteArray())) as ResponseRoot).xObj
      }
      fun xobjToXML(resp: XObj): String {
          val responseRoot = ResponseRoot(); responseRoot.xobj = resp
          val resMarsh = JAXBContext.newInstance(responseRoot.javaClass).createMarshaller()
          val resStream = ByteArrayOutputStream()
          resMarsh.marshal(responseRoot, resStream)
          return String(resStream.toByteArray())
      }
  }
  </code></pre>
  <pre><code>//autowired var feClient fetches info from other application
      @Test
      fun readWrite() {
          val rawResponse = feClient.fetchApp(&quot;AA&quot;, &quot;1A&quot;)
          val xmlString = Converter.xobjToXML(rawResponse)
          val parsedResponse = Converter.xmlToXObj(xmlString)
          val parsedTwiceXML = Converter.xobjToXML(parsedResponse!!)
          println(xmlString)
          println(parsedTwiceXML)
      }
  </code></pre>
  <p>now, to the real bulk of the issue:
  When I run the readWrite test, I lose the <code>Applicant</code> information from the <code>other.pkg.ApplicationObj</code>:</p>
  <pre><code>//println(xmlString)
  &lt;ns4:PersonAppResObj&gt;
              &lt;ns4:ApplicationObj&gt;
                  &lt;ns4:Applicant ns1:id=&quot;Applicant1&quot;&gt;
                  ...
                  &lt;ns3:PrimaryContact ns1:ref=&quot;primaryContact&quot;/&gt;
  
  </code></pre>
  <pre><code>//println(parsedTwiceXML)
  &lt;ns4:PersonAppResObj&gt;
              &lt;ns4:ApplicationObj&gt;
                  &lt;ns3:PrimaryContact ns1:ref=&quot;primaryContact&quot;/&gt;
                  //no applicant to be found
  </code></pre>
  <p>The error that comes up in console is
  <code>unexpected element (uri:&quot;...&quot;, local:&quot;Applicant&quot;). Expected elements are &lt;{...}PrimaryFilerPerson&gt;,&lt;{...}JointFilerPerson&gt;,&lt;{...}ApplicationPrimaryContact&gt;,&lt;{...}ApplicationMultipleIndicator&gt;,&lt;{...}Applicant&gt;</code></p>
  <p><strong>The Main Issue</strong></p>
  <p>There were two similar errors that came up in the console, and those elements were missing as well. When I looked at those objects in the generated class files, all 3 elements that didn't show up were annotated with <code>@XmlElementRef</code>.</p>
  <p>Firstly, what could be causing this issue? Is it just a case of the marshaller not finding the referenced objects?</p>
  <p>Second, is there a way to fix this issue without editing the generated objects at all? If not, what can I do without changing how the xml will look?</p>