title: How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session
body: <p>I get the following exception:</p>
  
  <pre class="lang-none prettyprint-override"><code>Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
      at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
      at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
      at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
      at sei.persistence.wf.entities.Element_$$_jvstc68_47.getNote(Element_$$_jvstc68_47.java)
      at JSON_to_XML.createBpmnRepresantation(JSON_to_XML.java:139)
      at JSON_to_XML.main(JSON_to_XML.java:84)
  </code></pre>
  
  <p>when I try to call from main the following lines:</p>
  
  <pre class="lang-java prettyprint-override"><code>Model subProcessModel = getModelByModelGroup(1112);
  System.out.println(subProcessModel.getElement().getNote());
  </code></pre>
  
  <p>I implemented the <code>getModelByModelGroup(int modelgroupid)</code> method firstly like this :</p>
  
  <pre class="lang-java prettyprint-override"><code>public static Model getModelByModelGroup(int modelGroupId, boolean openTransaction) {
  
      Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();     
      Transaction tx = null;
  
      if (openTransaction) {
          tx = session.getTransaction();
      }
  
      String responseMessage = "";
  
      try {
          if (openTransaction) {
              tx.begin();
          }
          Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");
          query.setParameter("modelGroupId", modelGroupId);
  
          List&lt;Model&gt; modelList = (List&lt;Model&gt;)query.list(); 
          Model model = null;
  
          for (Model m : modelList) {
              if (m.getModelType().getId() == 3) {
                  model = m;
                  break;
              }
          }
  
          if (model == null) {
              Object[] arrModels = modelList.toArray();
              if (arrModels.length == 0) {
                  throw new Exception("Non esiste ");
              }
  
              model = (Model)arrModels[0];
          }
  
          if (openTransaction) {
              tx.commit();
          }
  
          return model;
  
     } catch(Exception ex) {
         if (openTransaction) {
             tx.rollback();
         }
         ex.printStackTrace();
         if (responseMessage.compareTo("") == 0) {
             responseMessage = "Error" + ex.getMessage();
         }
         return null;
      }
  }
  </code></pre>
  
  <p>and got the exception. Then a friend suggested me to always test the session and get the current session to avoid this error. So I did this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static Model getModelByModelGroup(int modelGroupId) {
      Session session = null;
      boolean openSession = session == null;
      Transaction tx = null;
      if (openSession) {
          session = SessionFactoryHelper.getSessionFactory().getCurrentSession(); 
          tx = session.getTransaction();
      }
      String responseMessage = "";
  
      try {
          if (openSession) {
              tx.begin();
          }
          Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");
          query.setParameter("modelGroupId", modelGroupId);
  
          List&lt;Model&gt; modelList = (List&lt;Model&gt;)query.list(); 
          Model model = null;
  
          for (Model m : modelList) {
              if (m.getModelType().getId() == 3) {
                  model = m;
                  break;
              }
          }
  
          if (model == null) {
              Object[] arrModels = modelList.toArray();
              if (arrModels.length == 0) {
                  throw new RuntimeException("Non esiste");
              }
  
              model = (Model)arrModels[0];
  
              if (openSession) {
                  tx.commit();
              }
              return model;
          } catch(RuntimeException ex) {
              if (openSession) {
                  tx.rollback();
              }
              ex.printStackTrace();
              if (responseMessage.compareTo("") == 0) {
                  responseMessage = "Error" + ex.getMessage();
              }
              return null;        
          }
      }
  }
  </code></pre>
  
  <p>but still, get the same error. 
  I have been reading a lot for this error and found some possible solutions. One of them was to set lazyLoad to false but I am not allowed to do this that's why I was suggested to control the session</p>