title: Using POJO with nested classes with JAXB and Solr annotations
body: <p>I would like to have as few classes as possible, so I would like to construct <strong>a JAXB and Solr annotated POJO</strong>, as you might guess, to consume XML, marshall it to a Company object and index it.</p>
  
  <p>I have no problem with marshalling - the Company object gets constructed just fine. The problem I am having is how to annotate the Company POJO to create a <strong>multivalue string solr field</strong>.</p>
  
  <p>The XML looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;company&gt;
      &lt;registryNumber&gt;5226805000&lt;/registryNumber&gt;
      &lt;names&gt;
          &lt;name&gt;name1&lt;/name&gt;
          &lt;name&gt;name2&lt;/name&gt;
      &lt;/names&gt;
  &lt;/company&gt;
  </code></pre>
  
  <p>and my current pathetic attempt for the Company POJO looks like:</p>
  
  <pre><code>@XmlRootElement(name = "company")
  public class Company {
  
      public Company() {}
  
      @XmlElement
      @Field("id")
      public String registryNumber; 
  
      public static class Names {
          public Names() {}
  
          @XmlElement(name = "name")
          @Field("title")
          public List&lt;Name&gt; name;
  
          public static class Name {
              public Name() {}
  
              @XmlValue
              public String value;
          }
      }
  
      @XmlElement(name = "names")
      public Names names;
  
  }
  </code></pre>
  
  <p>The two unit test I have crafted:</p>
  
  <pre><code>public class CompanySearchTest extends AbstractSolrTestCase {
  
      private SolrServer server;
  
      private Company company;
  
      @Before
      @Override
      public void setUp() throws Exception {
          super.setUp();
          server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName());
  
          company = new Company();
          company.registryNumber = "5226805000";
          Company.Names.Name name1 = new Company.Names.Name();
          name1.value = "name1";
          Company.Names.Name name2 = new Company.Names.Name();
          name1.value = "name2";
          Company.Names names = new Company.Names();
          names.name = Arrays.asList(name1, name2);
  
          server.addBean(company);
          server.commit();
      }
  
      @After
      public void destroy() {
          h.getCoreContainer().shutdown();
      }
  
      @Test
      public void searchByIdTest() throws IOException, SolrServerException {
          SolrQuery query = new SolrQuery();
          query.setQuery("id:5226805000");
  
          QueryResponse response = server.query(query);
  
          List&lt;Company&gt; companiesFound = response.getBeans(Company.class);
  
          assertEquals(1L, companiesFound.size());
          assertEquals("5226805000", companiesFound.get(0).registryNumber);    
      }
  
      @Test
      public void searchByMultivalueNameTest() throws IOException, SolrServerException {
          SolrQuery query = new SolrQuery();
          query.setQuery("title:name1");
  
          QueryResponse response = server.query(query);
  
          List&lt;Company&gt; companiesFound = response.getBeans(Company.class);
  
          assertEquals(1L, companiesFound.size());
          assertEquals("name1", companiesFound.get(0).names.name.get(0).value);
      }
  
      @Override
      public String getSchemaFile() {
          return "solr/collection1/conf/schema.xml";
      }
  
      @Override
      public String getSolrConfigFile() {
          return "solr/collection1/conf/solrconfig.xml";
      }
  }
  </code></pre>
  
  <p>The test searchByIdTest() passes, while the searchByMultivalueNameTest() miserably fails as no company is found by searching with:</p>
  
  <pre><code>query.setQuery("title:name1");
  </code></pre>
  
  <p>The solr schema I am using is the provided with solr 4.0.0 (collection 1 example).</p>
  
  <p>Can someone give me some hints on <strong>how</strong>, if at all possible, <strong>should I annotate the Company POJO</strong> or what modifications should I make. </p>
  
  <p>The goal is to have <code>&lt;name&gt;name1&lt;/name&gt;</code> and <code>&lt;name&gt;name2&lt;/name&gt;</code> indexed as multivalue solr string field as defined in example solr schema with the:</p>
  
  <pre><code>&lt;field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/&gt;
  </code></pre>
  
  <p>so the company can be found by searching "title:name1" or by "title:name2".</p>
  
  <p>Many thanks!</p>