title: Jackson Circular Dependencies
body: <p>I have a circular dependency that I am struggling to solve right now
  Take these two classes - boiler plate code removed for demo purposes</p>
  
  <p>Class 1</p>
  
  <pre><code>@Entity
  @Table(name = "T_CREDENTIAL")
  @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
  @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
  public class Credential implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
  
      //Removed @JsonIgnore as want to disable certain fields if no credentials are available    
      //Also fetch this in an eager fashion instead of lazily loading it
      @OneToMany(mappedBy = "credential",fetch=FetchType.EAGER)
      private Set&lt;UserTask&gt; userTasks = new HashSet&lt;&gt;();
  
      ....
  
      .....
  </code></pre>
  
  <p>Class 2</p>
  
  <pre><code>@Entity
  @Table(name = "T_USERTASK")
  @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
  @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
  public class UserTask implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = -8179545669754377924L;
  
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Long id;
  
  
      @ManyToOne(fetch=FetchType.EAGER)
      @NotNull(message = "Credential must not be null")
      private Credential credential;
  </code></pre>
  
  <p>Unfortunately I have use cases where UserTask needs to load the credentials and cases where Credential needs to load the Usertasks</p>
  
  <p>The annotation @JsonIdentityInfo seems to be doing its job
  If i load all UserTasks, it loads the first userTask and its credential, but then that credential object will also load any UserTasks that are assigned to that credential. This then encounters the new @Id or a userTask which now loads it with the credential instead of as 2 users tasks</p>
  
  <p>Any ideas what I can do to circumvent this problem?</p>
  
  <p>Cheers
  Damien</p>
  
  <p>--Question Update
  I have updated my code now to use the new annotations as mentioned by other users</p>
  
  <p>Class 1</p>
  
  <pre><code>    @Entity
  @Table(name = "T_CREDENTIAL")
  @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
  public class Credential implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = -8696943454186195541L;
  
  
  
      //Removed @JsonIgnore as want to disable certain fields if no credentials are available    
      //Also fetch this in an eager fashion instead of lazily loading it
      @OneToMany(mappedBy = "credential",fetch=FetchType.EAGER)
      @JsonManagedReference("credential")
      private Set&lt;UserTask&gt; userTasks = new HashSet&lt;&gt;();
      ....
  ....
  </code></pre>
  
  <p>Class 2</p>
  
  <pre><code>@Entity
  @Table(name = "T_USERTASK")
  @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
  public class UserTask implements Serializable {
  
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Long id;
  
      @ManyToOne(fetch=FetchType.EAGER)
      @NotNull(message = "Credential must not be null")
      @JsonBackReference("credential")
      private Credential credential;
  ....
  ....
  </code></pre>
  
  <p>These classes now now work and have no circular dependencies
  However, when i do a query now to get my usertasks or a single task, the credential object is not returned even though I do not have the @jsonIgnore annotation specified. Any ideas what is causing this?</p>