title: Correct way to handle the session in hibernate
body: <p><strong>My code works</strong>, I just want to know if it's well written and what I should modify.
  I used different method to do MySQL selects, with hibernate HQL and with Criteria. Mainly to learn to use them.
  Another question is about Hibernate session, I don't know if I'm using it in the proper way.</p>
  
  <p>I've this session creation class:</p>
  
  <pre><code>public class HibernateUtil {
  
      private static final SessionFactory sessionFactory = buildSessionFactory();
  
      private static SessionFactory buildSessionFactory() {
          try {               
              //VER HIB 4
              Configuration configuration = new Configuration().configure();
              StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
              applySettings(configuration.getProperties());
  
              SessionFactory sf = configuration.buildSessionFactory(builder.build());
  
              return sf;          
          }
          catch (Throwable ex) {
              // Make sure you log the exception, as it might be swallowed
              System.err.println("Initial SessionFactory creation failed." + ex);
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
  }
  </code></pre>
  
  <p>And I've created a DAO class:</p>
  
  <pre><code>public class CreazioneUtenzaDAO extends CommonDAO
  {
  
      public CreazioneUtenzaDAO() {
  
      }
  
      public List&lt;Comune&gt; getComuni()
      {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          List&lt;Comune&gt; com = session.createQuery("FROM Comune").list(); 
          session.getTransaction().commit();
  
          return com;
      }
  
      public List&lt;Ente&gt; getEntiFromComune(Comune comune) {
          SessionFactory sf = HibernateUtil.getSessionFactory();
          Session session = sf.openSession();
          Criteria cri = session.createCriteria(Ente.class, "ente");
          //join on ente.tipoufficios as tipouff
          cri.createAlias("ente.tipoufficios", "tipouff");
          cri.createAlias("tipouff.ufficios", "uff");
          cri.add(Restrictions.eq("uff.comune", comune));
          cri.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);        
          List &lt;Ente&gt; en = cri.list();
          session.close();
  
          return en;
      }
  
      public List&lt;Tipoufficio&gt; getTipoufficioFromComuneEnte(String idComune, String idEnte) 
      {
          String strSql ="SELECT DISTINCT tu "+
                  "FROM Tipoufficio tu INNER JOIN tu.ufficios as u "+ 
                  "WHERE u.comune.codComune= :idComune AND tu.ente.idEnte= :idEnte "
                  +"order by tu.descrizione";
  
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          Query query = session.createQuery(strSql);
          query.setParameter("idComune", idComune);
          query.setParameter("idEnte", idEnte);
  
          List&lt;Tipoufficio&gt; list = query.list();
          session.close();
          return list;
      }
  
      public List&lt;Ufficio&gt; getUfficiFromComuneEnteTipoufficio(String idComune,
              String idEnte, String codTipoUfficio) {
          String strSql ="SELECT DISTINCT u  "+
                  "FROM Ufficio as u INNER JOIN u.tipoufficio as tu "+ 
                  "WHERE u.comune.codComune= :idComune AND tu.ente.idEnte= :idEnte AND tu.idTipoUfficio= :codTipoUfficio "
                  +" order by u.descrizione ";
  
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          Query query = session.createQuery(strSql);
          query.setParameter("idComune", idComune);
          query.setParameter("idEnte", idEnte);
          query.setParameter("codTipoUfficio", codTipoUfficio);
          List&lt;Ufficio&gt; list = query.list();
          session.close();
          return list;
      }
  
      public List&lt;Email&gt; getEmails() 
      {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          List&lt;Email&gt; tmp = session.createQuery("FROM Email").list();         
          session.getTransaction().commit();
          return tmp;
      }
  
      public List&lt;Applicazione&gt; getApplicazioni() 
      {
          return selectAll("Applicazione");
      }
  
      public List&lt;Qualifica&gt; getQualifiche() {
          return selectAll("Qualifica");
      }
  }
  </code></pre>
  
  <p>Are the way in which I open, close, begin and commit the transaction right? Or I should for example open and close the session just on time? Which is the best practice?</p>