So, my problem is I'm not sure whether my queries are being cached. The hibernate.xml that I have right now uses ehcache for the second level cache.
Right now this is my test code:
Code:
Session session = HibernateUtil.getCurrentSession();
Criteria criteria = session.createCriteria(Users.class);
criteria.add(Restrictions.eq("id", "777008"));
Users user = (Users) criteria.uniqueResult();
HibernateUtil.closeCurrentSession();
Session anotherSession = HibernateUtil.getCurrentSession();
Criteria anotherCriteria = anotherSession.createCriteria(Users.class);
anotherCriteria.add(Restrictions.eq("id", "777008"));
Users anotherUser = (Users) anotherCriteria.uniqueResult();
The `HibernateUtil.getCurrentSession()`method retrieves the current session in a `ThreadLocal` variable if it's available. If the session is null, then it creates one.
The `HibernateUtil.closeCurrentSession()` gets the session from the `ThreadLocal` variable and closes it if it's not null.
Whenever I run the code above (it's a JUnit test), I can only see one Hibernate log that SELECTS from the database. I was expecting there to be two since I'm not forcefully caching anything by using `Criteria.setCaching(true)`.
I believe that first level caches only exist within the session scope and second level caches are basically application caches. In the middle of the two `criteria.uniqueResult()` invocations above, I close the session.
My question is, why am I only seeing one Hibernate SELECT statement log? Shouldn't I be seeing two?
Also, when I go to my hibernate.xml and disable second level caching, the same output still appears which is what really confuses me alot.
Example of SELECT statement log that I see: `Hibernate: select this_.id as id8_0_, this_.nameas name8_0_, from users this_ where this_.id=?
`