title: How to save an object with null dbref in mongodb java spring?
body: <p>I'm looking for a solution to save an object with a null dbref in mongodb java spring framework. Consider the following example:</p>
  
  <pre><code>@Document
  public class A {
      @Id
      private String id;
      @DBRef
      private B b;
  
      public A() {
          this.b = null;
      }
  
      ...
  }
  
  @Document
  public class B {
      @Id
      private String id;
  }
  </code></pre>
  
  <p>Now if I instantiate A, i.e. <code>A a = new A();</code> and save this object to mongodb via repository, i.e. <code>aRepo.save(a)</code>. Then, I have the following exception:</p>
  
  <pre><code>org.springframework.data.mapping.model.MappingException: Cannot create a reference to an object with a NULL id.
  </code></pre>
  
  <p>Is there a way to save an object with a null dbref?</p>
  
  <p>Thanks for your help!</p>