I was using second level cache of hibernate, and the following code, the content in my dao method, works. By that I mean if i repeatly run this dao method, only the first SQL was sent to the DB.

try {
Session session = sessionFactory.openSession();
String queryString = "from User u where u.name=:username";
Query q = session.createQuery(queryString);
q.setParameter("username", username);
q.setCacheable(true); // <---- enable query cache
return (User)q.uniqueResult();
}
catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessExceptio n(e);
}


But, the follow code does not work. I tried to enable the query cache:

HibernateTemplate template = new HibernateTemplate(sessionFactory);
template.setCacheQueries(true); // <--- enable query cache
return (User)template.execute(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
return session.createQuery("from User u where u.name=:userName")
.setParameter("userName", username)
.uniqueResult(); }
}
);

Did i do anything wrong?

Thanks for help in advance...