title: Spring Data MongoDB @DBRef Loads null for child class
body: <p>I'm trying to save and load an object containing a @DBRef to another object that happens to be of a child class type and Spring Data MongoDB loads the field as null.</p>
  <pre class="lang-java prettyprint-override"><code>class Person {
      @Id
      private long id;
      @DBRef
      private Food food;
  }
  
  class Food {
      @Id
      private long id;
  }
  
  class Burger extends Food {}
  </code></pre>
  <p>I'm saving the objects separately:</p>
  <pre class="lang-java prettyprint-override"><code>Burger burger = new Burger();
  foodRepository.save(burger);
  
  Person person = new Person();
  person.setFood(burger);
  personRepository.save(person);
  </code></pre>
  <p>What happens is that the <code>burger</code> object gets saved in the <code>food</code> collection with the <code>_class</code> value of <code>Burger</code> in MongoDB and the <code>$ref</code> in the Person document points to <code>burger</code> and not <code>food</code>.</p>
  <p><code>person</code> collection:</p>
  <pre><code>{
      &quot;_id&quot; : NumberLong(1),
      &quot;food&quot; : {
          &quot;$ref&quot; : &quot;burger&quot;,
          &quot;$id&quot; : NumberLong(2)
      },
      &quot;_class&quot; : &quot;Person&quot;
  }
  </code></pre>
  <p><code>food</code> collection:</p>
  <pre><code>{
      &quot;_id&quot; : NumberLong(2),
      &quot;_class&quot; : &quot;Burger&quot;
  }
  </code></pre>
  <p>If I load the object using <code>findAll()</code> or <code>findById()</code>, the <code>food</code> field is null. But if i use <code>findByFood()</code> with the burger object, the person object is returned. What am I not understanding correctly here?</p>
  <pre class="lang-java prettyprint-override"><code>personRepository.findById(1L).getFood(); // null
  personRepository.findByFood(burger);     // Person object
  </code></pre>