-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate caching using Criteria queries
PostPosted: Tue Mar 31, 2015 3:49 pm 
Newbie

Joined: Tue Mar 31, 2015 3:47 pm
Posts: 3
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=?
`


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.