title: Select using hibernate
body: <p>I saw few examples over internet of Hibernate using <code>transaction.commit()</code> for select statements. Below is the example code.</p>
  
  <pre><code>public static List&lt;?&gt; list(Class&lt;?&gt; className,int start,int limit,SearchFilter[] searchFilter){
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null; 
  
      try {
          transaction = session.beginTransaction();
  
          Criteria criteria = session.createCriteria(className);
          criteria.setFirstResult(start);
          criteria.setMaxResults(limit);
  
          for(SearchFilter sf : searchFilter){
              String[] values = sf.getValue();
              if(values != null){
                  if(values.length == 1) {
                      criteria.add(Restrictions.eq(sf.getField(), values[0]));
                  }else{
                      criteria.add(Restrictions.in(sf.getField(), values));
                  }
              }
          }
  
          List&lt;?&gt; Objects = criteria.list();
          transaction.commit();
  
          return Objects;
      }catch (Exception e) {
          transaction.rollback();
          e.printStackTrace();
      }finally{
          session.close();
      }
  
      return null;
  }
  </code></pre>
  
  <p>Why do we do beginning and committing a transaction for select statement?</p>