title: Spring MVC 3.1.2 + Jackson 2: LazyInitializationException when lazily initialize a collection - no session or session was closed
body: <p>I'm getting crazy with an error that I'm having using Spring MVC 3.1.2 and Jackson 2.</p>
  
  <p>I have the following model Class:</p>
  
  <pre><code>@Entity
  @Table(name = "USER")
  @JsonIgnoreProperties(ignoreUnknown=true)
  public class User implements Serializable
  {
      @Id
      @SequenceGenerator(name = "USER_ID", sequenceName = "USER_ID_SEQ", allocationSize = 1)
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_ID")
      private Long id;
  
      @Column(length = 50, nullable = false)
      private String firstName;
  
      @Column(length = 50, nullable = false)
      private String lastName;
  
      @ManyToMany
      @JoinTable(name = "FRIENDS",
          joinColumns = @JoinColumn(name = "personId"),
          inverseJoinColumns = @JoinColumn(name = "friendId")
      )
      @JsonManagedReference
      private List&lt;User&gt; friends;
  
      @ManyToMany
      @JoinTable(name="FRIENDS",
          joinColumns=@JoinColumn(name="friendId"),
          inverseJoinColumns=@JoinColumn(name="personId")
      )
      @JsonIgnore
      private List&lt;User&gt; friendOf;
  
      // Other attributes and methods... 
  }
  </code></pre>
  
  <p>When I get an single instance of User it is correctly serialized by Jackson. But when I try to get an instance of User that contains friends, the following exception is thrown:</p>
  
  <blockquote>
    <p><strong>org.hibernate.LazyInitializationException: failed to lazily initialize
    a collection of role: com.frooid.model.User.friends, no session or
    session was closed</strong></p>
  </blockquote>
  
  <p>I'm getting this instance using a single HQL:</p>
  
  <pre><code>select u from User u left join fetch u.friends f where u.id = :id
  </code></pre>
  
  <p>Thanks to everybody!</p>