title: Lazy Loading in Java Spring exception
body: <p>In my controller I have the following code getting a specific presentation in my database, adding it as a model-attribute and returning the detail view:</p>
  
  <pre><code>@RequestMapping(value = "/detail", method = RequestMethod.GET)
  public String detail(Model model, Principal principal, @RequestParam("id") int id) {
          Presentation p = presentationDao.get(id);
          model.addAttribute("presentation", p);
          return "detail";
  }
  </code></pre>
  
  <p>In my view I'm trying to display an attribute of the Presentation that has lazy loading using JSTL:</p>
  
  <p>${pressentation.student.dossiers.proposal.titel}</p>
  
  <p>However in Student there is a list of 'Dossiers', but it is using Lazy Loading: </p>
  
  <pre><code>@OneToMany(mappedBy = "student", fetch = FetchType.LAZY)
  private List&lt;Dossier&gt; dossiers;
  </code></pre>
  
  <p>but I get the following exception:</p>
  
  <pre><code>org.apache.jasper.JasperException: javax.el.ELException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: domein.Student.dossiers, no session or session was closed
  </code></pre>
  
  <p>When I set FetchType of dossiers to Eager I can't even run my project:</p>
  
  <pre><code>Severe:   Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
  </code></pre>
  
  <p>When I googled I found the following solution:</p>
  
  <pre><code>@RequestMapping(value = "/detail", method = RequestMethod.GET)
      public String detail(Model model, Principal principal, @RequestParam("id") int id) { 
          Presentatie p = presentatieDao.get(id);
          Hibernate.initialize(p.getStudent().getDefinitiefDossier().getVoorstel().getTitel());
          model.addAttribute("presentatie", p);
          return "detail";
  }
  </code></pre>
  
  <p>but it's giving me the follow exception again:</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: domein.Student.dossiers, no session or session was closed
  </code></pre>