title: LazyInitializationException in Hibernate : could not initialize proxy - no Session
body: <p>I call dao from my service as </p>
  
  <pre><code>@Override
  @Transactional
  public Product getProductById(int id) {
      return productDao.getProductById(id);
  }
  </code></pre>
  
  <p>and in the dao I am getting product as </p>
  
  <pre><code>@Override
  public Product getProductById(int id) {
      Product p = sessionFactory.getCurrentSession().load(Product.class, id);
      System.out.print(p);
      return p;
  }
  </code></pre>
  
  <p>This runs fine but if I change my dao class to</p>
  
  <pre><code>@Override
  public Product getProductById(int id) {
      return sessionFactory.getCurrentSession().load(Product.class, id);
  }
  </code></pre>
  
  <p>I get org.hibernate.LazyInitializationException: could not initialize proxy - no Session. The exception occurs in view layer where I am just printing the product. I do not understand why returning in same line in dao method results in exception in view layer but works fine if I save it in a reference and then return that.</p>