title: Could not write JSON: failed to lazily initialize a collection of role:
body: <p>i am having an error with the fetch type and i dont know how to fix it! Please help me if u can. :D. Using java 8</p>
  <p>THE COMMAND LINE RUNNER:</p>
  <pre><code>    @Autowired CustomTableRepository tr;
      @Autowired UserRepository ur;
      @Autowired RoleRepository rr;
      @Bean
      CommandLineRunner commandLineRunner() {
          return args -&gt; {
              tr.save(new CustomTable(null, true, null));
              tr.save(new CustomTable(null, true, null));
              tr.save(new CustomTable(null, true, null));
              tr.save(new CustomTable(null, true, null));
              tr.save(new CustomTable(null, true, null));
  
              ur.save(new User(null, &quot;cpthermes&quot;, &quot;thanatos&quot;, 123987456l, 3123231l, null,null));
              ur.save(new User(null, &quot;moni1008&quot;, &quot;milky&quot;, 123987456l, 31232131l, null, null));
              ur.save(new User(null, &quot;mario&quot;, &quot;zoro123&quot;, 1231231l, 32123l, null, null));
  
              
          };
      }
  </code></pre>
  <p>THE MODEL CLASSES:</p>
  <pre><code>@Entity
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public class Reservation {
      
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private Long id;
      private Boolean accepted;
      @ManyToOne
      @JoinColumn(name = &quot;user_id&quot;)
      private User user;
      @OneToOne
      @JoinColumn(name = &quot;table_id&quot;)
      private CustomTable table;
      private LocalTime time;
  }
  
  
  
  @Entity
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public class User {
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private Long id;
      private String username;
      private String password;
      private Long number;
      private Long balance;
      @OneToMany(mappedBy = &quot;user&quot;, cascade = CascadeType.ALL, orphanRemoval = true)
      private List&lt;Reservation&gt; reservations;
      @ManyToMany
      private Collection&lt;Role&gt; roles;
  }
  
  @Entity
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public class Role {
      @Id
      @GeneratedValue
      private Long id;
      private String type;
  }
  </code></pre>
  <p>THE ERROR:</p>
  <pre><code>Could not write JSON: failed to lazily initialize a collection of role: com.mile.pc.mile.restoraunt.app.model.User.reservations, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.mile.pc.mile.restoraunt.app.model.User.reservations, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]-&gt;com.mile.pc.mile.restoraunt.app.model.User[&quot;reservations&quot;])
  org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.mile.pc.mile.restoraunt.app.model.User.reservations, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.mile.pc.mile.restoraunt.app.model.User.reservations, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]-&gt;com.mile.pc.mile.restoraunt.app.model.User[&quot;reservations&quot;])
  </code></pre>
  <p>....................
  ....................
  ....................
  ....................
  ....................
  ....................
  Ty for reading.</p>