title: LazyInitializationException Spring Boot
body: <p>I know there are a lot of similar threads out there but i just can't figure it out from those threads on how to overcome this problem.</p>
  
  <p>I have 3 classes Car, Brand, Color.
  A Car has just one Brand and a list of Colors.
  Brand has a List of Cars.
  Color does not have any relation.</p>
  
  <p>Getters, Setters, ToString and Constructors are not provided for simplicity sake.
  I'm able to save objects into database and database is already populated.</p>
  
  <h2>--------------------------------------------------------------------------------</h2>
  
  <pre><code>@Entity
  @Table(catalog = "spring_project")
  public class Car {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  private String model;
  
  @ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinTable( name = "car_color", catalog = "spring_project",
              joinColumns         = { @JoinColumn(name = "car_id") },
              inverseJoinColumns  = { @JoinColumn(name = "colors_id") }
  )
  private List&lt;Color&gt; colors = new ArrayList&lt;&gt;();
  
  @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinColumn(name="brand_id", referencedColumnName="id")
  private Brand brand;
  </code></pre>
  
  <h2>--------------------------------------------------------------------------------</h2>
  
  <pre><code>@Entity
  @Table(catalog = "spring_project")
  public class Brand {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  private String name;
  
  @OneToMany(mappedBy = "brand", fetch = FetchType.LAZY)
  private List&lt;Car&gt; cars = new ArrayList&lt;&gt;();
  </code></pre>
  
  <h2>--------------------------------------------------------------------------------</h2>
  
  <pre><code>@Entity
  @Table(catalog = "spring_project")
  public class Color {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String name;
  </code></pre>
  
  <h2>--------------------------------------------------------------------------------</h2>
  
  <p>Everything runs just fine if i fetch like Eager, but i know it is a bad practice and it should be used Lazy loading instead. But i keep getting the LazyInitializationException.</p>
  
  <p>I understand from the error that a session is required but i dont know how to provide one since im working with Spring Data JPA neither where i should declare one...</p>
  
  <pre><code>@SpringBootApplication
  public class SrpingJpaApplication {
  
  private static final Logger log = 
  LoggerFactory.getLogger(SrpingJpaApplication.class);
  
  public static void main(String[] args) {
      SpringApplication.run(SrpingJpaApplication.class, args);
  }
  
  @Bean
  public CommandLineRunner demo(CarRepository carRepository,
                                ColorRepository colorRepository,
                                BrandRepository brandRepository) {
      return (args) -&gt; {
  
          log.info("Reads all cars....");
          for (Car c : carRepository.findAll()) {
              System.out.println(c.toString());
          }
  
      };
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Thank you so much.</p>
  
  <p>Edited----->>></p>
  
  <p>The error is thrown on c.toString();</p>
  
  <p>Error: Caused by: org.hibernate.LazyInitializationException: could not initialize 
  proxy [com.readiness.moita.SrpingJPA.Models.Brand#1] - no Session</p>