title: Jsonparsing error with @JsonIgnore on Hibernate
body: <p>I get this exception when Jackson tries to parse my data to Json:</p>
  
  <pre><code>org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: packagename.Thing.Stuffs, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]-&gt;packagename.Stuff[“thing"]-&gt;packagename.Thing[“stuffs"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: packagename.Thing.Stuffs, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]-&gt;packagename.Stuff[“thing"]-&gt;packagename.Thing[“stuffs"])
  </code></pre>
  
  <p>I have the following entities(names have been replaced with Stuff and Thing):</p>
  
  <p>Stuff:</p>
  
  <pre><code>@Entity
  @Table(name = "stuff")
  @SQLDelete(sql = "UPDATE stuff SET deleted = 1 WHERE id = ?")
  @Where(clause = "deleted = 0")
  public class Stuff implements Serializable {
  
      private Long id;
      private Thing thing;
      private String stuffName;
  
      @Id
      @Column(name = "id", unique = true, nullable = false)
      @GeneratedValue
      public Long getId() {
          return this.id;
      }
  
      @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, optional = false)
      @JoinColumn(name = "thing_id", nullable = false)
      public Thing getThing() {
          return thing;
      }  
  
      @Transient
      public String getStuffName() {
          return stuffName;
      }
  
      // Setters and constructor(s) omitted
  
  }
  </code></pre>
  
  <p>Thing:</p>
  
  <pre><code>@Entity
  @Table(name = "thing")
  public class Thing implements Serializable {
  
      private Long id;
      private String name;
      private List&lt;Stuff&gt; stuffs;
  
  
      @Id
      @Column(name = "id", unique = true, nullable = false)
      @GeneratedValue
      public Long getId() {
          return this.id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @Column(name = "name", unique = false, nullable = false, length = 45)
      public String getName() {
          return this.name;
      }
  
      @OneToMany(cascade = CascadeType.ALL)
      @JoinColumn(name = "thing_id")
      @JsonIgnore
      public List&lt;Stuffs&gt; getStuffs() {
          return stuffs;
      }
  
  
      // Setters and constructor(s) omitted
  }
  </code></pre>
  
  <p>I have been googling for this type of error. It seems that this is happening when the json-parser is trying to parse objects that aren't loaded due to lazy-load. Lazy load seems to be on by default. I'd like to avoid setting everything to eager so I put @JsonIgnore instead. Changes nothing for me. Help would be greatly appreciated, this is driving me nuts.</p>
  
  <p>EDIT:</p>
  
  <p>Adding @JsonIgnore and @Eager in both classes gives me another problem. An exception that is looking like this:</p>
  
  <pre><code>org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exception java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
  </code></pre>
  
  <p>Searching for that on stackoverflow gives me this: <a href="https://stackoverflow.com/questions/26572245/hibernate-and-jackson-java-lang-illegalstateexception-cannot-call-senderror">Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed)</a></p>
  
  <p>The link is basically saying that I should add @JsonIgnore, and I have already done that.</p>