title: Hibernate issue: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
body: <p>I found many times on stackoverflow this issue, but nothing from them gives me clear answer. For simplicity, there are only two tables <strong>film</strong> and <strong>language</strong> bound many to one relation. Everything done according <a href="https://netbeans.org/kb/docs/web/hibernate-webapp.html" rel="nofollow">Netbeans Hibernate DVD Store tutorial</a>. Now, how to display in first page (<strong>index.xhtml</strong>) language. It looks like very straightforward. Simply add:</p>
  
  <pre><code>                  &lt;h:column&gt;
                      &lt;f:facet name="header"&gt;
                          &lt;h:outputText value="Language"/&gt;
                      &lt;/f:facet&gt;
                      &lt;h:outputText value="#{item.languageByLanguageId.langName}"/&gt;
                   &lt;/h:column&gt;
  </code></pre>
  
  <p>(Comumn in table <strong>language</strong> <strong>name</strong> was renamed on <strong>langName</strong>)
  But it issues still the same LazyInitializationException. I tried to obtain <strong>languageId</strong> and in this case I was successful. It means <strong>#{item.languageByLanguageId.langName}</strong> gives exception but <strong>#{item.languageByLanguageId.languageId}</strong> not. It is strange. So what happen, when I use explicit fetch according <strong>languageId</strong> if I can obtain its.</p>
  
  <p>So I added in <strong>FilmController.java</strong> method for obtaining language:</p>
  
  <pre><code>  public String getLanguageById(Integer langId) {
      String language = helper.getLangById(langId);
      return language;
    }
  </code></pre>
  
  <p>And in <strong>FilmHelper.java</strong> (final version):</p>
  
  <pre><code>  public Film getFilmById(int filmId) {
  
  Film film = null;
  
  try {
      session = HibernateUtil.getSessionFactory().getCurrentSession();
      org.hibernate.Transaction tx = session.beginTransaction();
      Query q = session.createQuery("select count(film.filmId) from Film as film where film.filmId = :filmId");
      q.setParameter("filmId", filmId);
      Number count = (Number) q.uniqueResult();
      if (count.intValue() &gt; 0)
        film = (Film) session.load(Film.class, filmId);
      tx.commit();
  } catch (Exception e) {
      e.printStackTrace();
  }
  
  return film;
  </code></pre>
  
  <p>}</p>
  
  <p>And yes, it works, I can obtain language name to modify index.xhtml:</p>
  
  <pre><code>&lt;h:outputText value="{filmController.getLanguageById(item.languageByLanguageId.languageId)}"/&gt;
  </code></pre>
  
  <p>Than I tried to modify FilmActor.hbm.xml to add lazy="false" and use origin simple solution in <strong>index.xhtml</strong> (<strong>"#{item.languageByLanguageId.langName}"</strong>):</p>
  
  <pre><code>&lt;many-to-one name="languageByOriginalLanguageId" class="dvdrental.Language" lazy="false" fetch="select"&gt;
          &lt;column name="original_language_id" /&gt;
      &lt;/many-to-one&gt;
  </code></pre>
  
  <p>Again it works properly. Even if I set <strong>lazy="proxy"</strong> or <strong>lazy="no proxy"</strong>. But still I don't understand, how to use this default attribute <strong>lazy="true"</strong>. If I try to keep whole document in one session (don't do commit, which causes end of session), there is another Exception issue. It looks like, that <strong>lazy="true"</strong> doesn't meet in any time proper result.</p>