Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


select using hibernate


select using hibernate  using -'java,hibernate,orm'

I saw few examples over internet of Hibernate using transaction.commit() for select statements. Below is the example code.

public static List<?> list(Class<?> 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<?> Objects = criteria.list();
        transaction.commit();

        return Objects;
    }catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();
    }finally{
        session.close();
    }

    return null;
}


My question is why do we do beginning and committing a transaction for select statement?

Thanks.  
    

asked Oct 7, 2015 by vickeykumar66
0 votes
2 views



Related Hot Questions



Walkin Jobs Opening



Government Jobs Opening


...