title: JPA ManyToMany relationship and JSON serialization
body: <p>I'm implementing a RESTful service using Spring Framework v3.2.4 and JPA + Hibernate 4. 
  I'm returning resources in JSON format (using Jackson Mapper), but now I'm stuck with this exception:</p>
  
  <blockquote>
    <p><code>Could not write JSON: failed to lazily initialize a collection of role: it.teck.service.model.Canvas.params, could not initialize proxy - no Session (through reference chain: it.teck.service.model.Canvas["params"]);</code></p>
  </blockquote>
  
  <p>I have a "many to many" relationship between <code>Canvas</code> and <code>Param</code> entities and I need to serialize also params list when a canvas is requested to the service.</p>
  
  <p>In my classes I have:</p>
  
  <pre><code>@Entity
  public class Canvass {
  
      @ManyToMany
      @JoinTable(name = "canvas_params", joinColumns = { @JoinColumn(name = "id_canvas", referencedColumnName = "id_canvas") }, inverseJoinColumns = { @JoinColumn(name = "id_param", referencedColumnName = "id_param") })
          private List&lt;Param&gt; params;
  
      // ...
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>@Entity
  public class Param {
  
      @ManyToMany(mappedBy = "params")
      private List&lt;Canvas&gt; canvasList;
  
      // ...
  }
  </code></pre>
  
  <p>In several posts and SO answers is suggested to exclude <code>ManyToMany</code> fields from serialization, to break the serialization loop, but <strong>I need to serialize params linked to my canvas entities</strong>. So, what should I do?</p>