title: JAXB serialized XML with ID references rather than containment
body: <p>In a RestFul-Webservice (Jersey) context I need to marshal / serialize an Object graph to XML and JSON. For simplicity I try to explain the problem with 2-3 classes:</p>
  
  <p><em>Person.java</em></p>
  
  <pre><code>@XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Person {
  
      private String name;
  
      // @XmlIDREF
      @XmlElement(name = "house")
      @XmlElementWrapper(name = "houses")
      private Collection&lt;House&gt; houses;
  
      public Person() {}
  
      public Person(String name, Collection&lt;House&gt; houses) {
          this.name = name;
          this.houses = houses;
      }
  }
  </code></pre>
  
  <p><em>House.java</em></p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  public class House {
  
      // @XmlID
      public String name;
  
      public String location;
  
      public House() {}
  
      public House(String name, String location) {
          this.name = name;
          this.location = location;
      }
  }
  </code></pre>
  
  <p>Now when I serialize a Person, the XML will look like this:</p>
  
  <pre><code>&lt;people&gt;
      &lt;person&gt;
          &lt;name&gt;Edward&lt;/name&gt;
          &lt;houses&gt;
              &lt;house&gt;
                  &lt;name&gt;MyAppartment&lt;/name&gt;
                  &lt;location&gt;London&lt;/location&gt;
              &lt;/house&gt;
              &lt;house&gt;
                  &lt;name&gt;MySecondAppartment&lt;/name&gt;
                  &lt;location&gt;London&lt;/location&gt;
              &lt;/house&gt;
          &lt;/houses&gt;
      &lt;/person&gt;
  
      &lt;person&gt;
          &lt;name&gt;Thomas&lt;/name&gt;
          &lt;houses&gt;
              &lt;house&gt;
                  &lt;name&gt;MyAppartment&lt;/name&gt;
                  &lt;location&gt;London&lt;/location&gt;
              &lt;/house&gt;
              &lt;house&gt;
                  &lt;name&gt;MySecondAppartment&lt;/name&gt;
                  &lt;location&gt;London&lt;/location&gt;
              &lt;/house&gt;
          &lt;/houses&gt;
      &lt;/person&gt;
  &lt;/people&gt;
  </code></pre>
  
  <p>The problem here is, that the same houses are listed multiple times. Now I add the uncommented <code>XmlIDREF</code> and <code>XmlID</code> annotations, which would result in XML similar to this:</p>
  
  <pre><code>&lt;people&gt;
      &lt;person&gt;
          &lt;name&gt;Edward&lt;/name&gt;
          &lt;houses&gt;
              &lt;house&gt;MyAppartment&lt;/house&gt;
              &lt;house&gt;MySecondAppartment&lt;/house&gt;
          &lt;/houses&gt;
      &lt;/person&gt;
  
      &lt;person&gt;
          &lt;name&gt;Thomas&lt;/name&gt;
          &lt;houses&gt;
              &lt;house&gt;MyAppartment&lt;/house&gt;
              &lt;house&gt;MySecondAppartment&lt;/house&gt;
          &lt;/houses&gt;
      &lt;/person&gt;
  &lt;/people&gt;
  </code></pre>
  
  <p>While the first XML was too verbose, this one lacks of information. How can I create (and unmarshal) something similar to:</p>
  
  <pre><code>&lt;people&gt;
      &lt;person&gt;
          &lt;name&gt;Edward&lt;/name&gt;
          &lt;houses&gt;
              &lt;house&gt;MyAppartment&lt;/house&gt;
              &lt;house&gt;MySecondAppartment&lt;/house&gt;
          &lt;/houses&gt;
      &lt;/person&gt;
  
      &lt;person&gt;
          &lt;name&gt;Thomas&lt;/name&gt;
          &lt;houses&gt;
              &lt;house&gt;MyAppartment&lt;/house&gt;
              &lt;house&gt;MySecondAppartment&lt;/house&gt;
          &lt;/houses&gt;
      &lt;/person&gt;
  
      &lt;houses&gt;
          &lt;house&gt;
              &lt;name&gt;MyAppartment&lt;/name&gt;
              &lt;location&gt;London&lt;/location&gt;
          &lt;/house&gt;
          &lt;house&gt;
              &lt;name&gt;MySecondAppartment&lt;/name&gt;
              &lt;location&gt;London&lt;/location&gt;
          &lt;/house&gt;
      &lt;/houses&gt;
  &lt;/people&gt;
  </code></pre>
  
  <p>The solution should be generic because I don't want to write extra classes for each new element in the object graph.</p>
  
  <p>For completeness, here's the restful webservice:</p>
  
  <pre><code>@Path("rest/persons")
  public class TestService {
      @GET
      @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
      public Collection&lt;Person&gt; test() throws Exception {
          Collection&lt;Person&gt; persons = new ArrayList&lt;Person&gt;();
          Collection&lt;House&gt; houses = new HashSet&lt;House&gt;();
          houses.add(new House("MyAppartment", "London"));
          houses.add(new House("MySecondAppartment", "London"));
          persons.add(new Person("Thomas", houses));
          persons.add(new Person("Edward", houses));
          return persons;
      }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>