Wednesday, August 20, 2008

Query Caching using Detached criteria

//Create a criteria
DetachedCriteria criteria = DetachedCriteria.forClass(/*classname.class*/);
criteria = criteria.setProjection(
Projections.distinct(Projections.property(/*Criteria*/))).addOrder(Order.asc(/*Order by criteria*/));
// Enable query caching.
criteria.getExecutableCriteria(getSession()).setCacheable(true);
List /*list name*/= getHibernateTemplate().findByCriteria(criteria);


Using sessions :

session = HibernateSessionFactory.currentSession();
List /*list name*/= session.createCriteria(/*classname.class*/).setProjection( Projections.distinct(Projections.property(/*Criteria*/))).addOrder(Order.asc(/*Order by criteria*/))) .setCacheable(true).list();