title: How to Solve Lazy initialize Exception to get the list of data using hibernate Bidirectional One to Many Mapping
body: <p>Consider the following two tables - posts and comments of a Blog database schema where the posts table has a one-to-many relationship with the comments table</p>
  
  <p>I'm using <strong>Bidirectional One to Many Mapping</strong>, because my child entities are limited only.</p>
  
  <p>In <strong>Bidirectional One to Many Mapping</strong>, I wrote one rest call to get all the post with appropriate comments for the post Using default find all method in JPA Repository. </p>
  
  <p><strong>But the following exception occurred:</strong></p>
  
  <pre><code>"message": "Could not write JSON: failed to lazily initialize a collection of role: com.insights.apartmento.modal.Post.comments, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.insights.apartmento.modal.Post.comments, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]-&gt;com.insights.apartmento.modal.Post[\"comments\"])",
  </code></pre>
  
  <p>My Pojo class as follows</p>
  
  <blockquote>
  <pre><code>In POST POJO
  </code></pre>
  </blockquote>
  
  <pre><code>@Entity
  @Table(name = "posts")
  public class Post {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Size(max = 100)
      @Column(unique = true)
      private String title;
  
      @Size(max = 250)
      private String description;
  
      @Lob
      private String content;
  
      @OneToMany(cascade = CascadeType.ALL,
              fetch = FetchType.LAZY,
              mappedBy = "post")
      private Set&lt;Comment&gt; comments = new HashSet&lt;&gt;();
  
      // Getters and Setters (Omitted for brevity)
  }
  </code></pre>
  
  <blockquote>
    <p>In Comments POJO</p>
  </blockquote>
  
  <pre><code>@Entity
  @Table(name = "comments")
  public class Comment{
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @NotNull
      @Lob
      private String text;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "post_id", nullable = false)
      private Post post;
  
      // Getters and Setters (Omitted for brevity)
  }
  </code></pre>