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);

        for(SearchFilter sf : searchFilter){
            String[] values = sf.getValue();
            if(values != null){
                if(values.length == 1) {
                    criteria.add(Restrictions.eq(sf.getField(), values[0]));
                    criteria.add(, values));

        List<?> Objects = criteria.list();

        return Objects;
    }catch (Exception e) {

    return null;

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


asked Oct 11, 2015 by sanjaypal1983
0 votes

1 Answer

0 votes

everything happens within the scope of a transaction. sometimes software automatically manages a transaction for you, but hibernate does not. whether read-only or not, in hibernate you must open and close transactions.

answered Oct 11, 2015 by rajesh