title: Resolving Circular Reference While Serializing with Jackson: Bidirectional
body: <p>I have a situation where there in circular reference between two objects and that results in a  error while serializing any of the objects using jackson. Here is the sample situation:</p>
  
  <pre><code>Class A {
      private B b;
  }
  Class B {
      private A a;
  }
  </code></pre>
  
  <p>Jackson provides a solution for this situation in the form of <code>@JsonManagedReference</code> and <code>@JsonBackReference</code> annotations. While the solution works wonderfully well for the case where one wants reference of only one of the classes in the other class in the resulting json. For an example, following code works:</p>
  
  <pre><code>Class A {
      @JsonManagedReference
      private B b;
  }
  Class B {
      @JsonBackReference
      private A a;
  }
  </code></pre>
  
  <p>This will have the reference of B while trying to serialize A.</p>
  
  <p>I want the reverse to be correct as well. I want the field b when I am serializing A as well as the field a when I am serializing B. This is what I have tried.</p>
  
  <pre><code>Class A {
      @JsonManagedReference("abc")
      @JsonBackReference("xyz")
      private B b;
  }
  Class B {
      @JsonBackReference("abc")
      @JsonManagedReference("xyz")
      private A a;
  }
  </code></pre>
  
  <p>This doesn't work. How can this be achieved?</p>