title: Deserializing JSON object bi-directional one-to-many with Jackson ObjectMapper
body: <p>I am trying to deserialize an JSON object like</p>
  <pre><code>{ 
    &quot;name&quot;:&quot;aaa&quot;,
    &quot;children&quot;: [ 
      {&quot;name&quot;:&quot;bbb&quot;}
    ]
  }
  </code></pre>
  <p>Into Java objects where the child has a reference to the parent object, e.g.:</p>
  <pre class="lang-java prettyprint-override"><code>public class Parent {
    public String name;
    public List&lt;Child&gt; children;
  }
  public class Child {
    public String name;
    public Parent parent;
  }
  
  // ...
  new ObjectMapper().readValue(&lt;JSON&gt;, Parent.class);
  </code></pre>
  <p>When deserializing it like this, <code>Child#parent</code> will not point back to the parent object.
  I read about two approaches while doing my online research, but non seems to work.</p>
  <p><strong>1. Adding a constructor arg to the <code>Child</code> class to set the parent object</strong></p>
  <pre class="lang-java prettyprint-override"><code>public class Child {
    public String name;
    public Parent parent;
    public Child(Parent parent) {
      this.parent = parent;
    }
  }
  </code></pre>
  <p>When doing this I get the error:</p>
  <pre><code>Exception in thread &quot;main&quot; com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 
    Cannot construct instance of `Child` (no Creators, like default construct, exist): 
    cannot deserialize from Object value (no delegate- or property-based Creator)
    at [Source: (String)&quot;{&quot;name&quot;:&quot;aaa&quot;,&quot;children&quot;:[{&quot;name&quot;:&quot;bbb&quot;}]}&quot;; line: 1, column: 27] 
    (through reference chain: Parent[&quot;children&quot;]-&gt;java.util.ArrayList[0])
  </code></pre>
  <p><strong>2. Using the <code>@JsonBackReference</code> and <code>@JsonManagedReference</code> annotations</strong></p>
  <pre class="lang-java prettyprint-override"><code>public class Parent {
    public String name;
    @JsonBackReference
    public List&lt;Child&gt; children;
  }
  public class Child {
    public String name;
    @JsonManagedReference
    public Parent parent;
  }
  </code></pre>
  <p>This fails with:</p>
  <pre><code>Exception in thread &quot;main&quot; com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 
    Cannot handle managed/back reference 'defaultReference':
    back reference type (java.util.List) not compatible with managed type (Child)
    at [Source: (String)&quot;{&quot;name&quot;:&quot;aaa&quot;,&quot;children&quot;:[{&quot;name&quot;:&quot;bbb&quot;}]}&quot;; line: 1, column: 1]
  </code></pre>
  <p>The JavaDoc of <code>@JsonBackReference</code> says it cannot be applied to collections so it obviously does not work, but I wonder why there are so many examples online where it is applied to a collection.</p>
  <p><strong>Question</strong>
  How can I achieve that a child object get's its parent/owner object automatically set when the object graph is deserialized. I actually would prefer to somehow get the first approach working somehow, as it does not pollute require to pollute the classes with framework specific annotations.</p>