title: Hibernate session error in Vaadin
body: <p>I´m developing a Vaadin webapp using <a href="https://vaadin.com/book/-/page/jpacontainer.html" rel="nofollow">JPAContainer</a> with hibernate 4.3.
  I followed the instructions of this <a href="https://vaadin.com/book/-/page/jpacontainer.hibernate.html" rel="nofollow">link</a> and implemented EntityManager-per-Request pattern successfully. That means the lazy loading works well with JPAContainer</p>
  
  <p>However, every time I need to use pure JPA, it fails to lazy loads.</p>
  
  <p>Here is an example code:</p>
  
  <pre><code> try {
      entityManagerProvider.getEntityManager().getTransaction().begin();
  
      List&lt;SubEntity&gt; subEntitiesList = myEntity.getCollectionOfEntities(); //Fails to load
  
      for (SubEntity subEntity : subEntitiesList) {
          subEntity.doSomething();
          entityManagerProvider.getEntityManager().merge(subEntity);
      }
      entityManagerProvider.getEntityManager().flush();
      entityManagerProvider.getEntityManager().getTransaction().commit();
  } catch (Exception ex) {
      entityManagerProvider.getEntityManager().getTransaction().rollback();
  }
  
  //datasource is JPAContainer&lt;MyEntity&gt;  type
  datasource.removeItem(index);
  </code></pre>
  
  <p>I have tried this solutions:</p>
  
  <ol>
  <li><p>Use hibernate.enable_lazy_load_no_trans
  It opened an session automatically, but it gave a serious warning: </p>
  
  <p>"Unable to close temporary session used to load lazy collection associated to no session"</p></li>
  </ol>
  
  <p>Also, when I remove the Entity from the JPAContainer, I receive an error: org.hibernate.PersistentObjectException: detached entity passed to persist</p>
  
  <p>Can someone give me some hints on how to deal with this problem?</p>