balboa_21
balboa_21

Reputation: 384

Hibernate Query Cache , Behavior is not as expected

I just went through this article and this which I found will improve the performance of my application as it had 99% read operations only. I implemented it in test application first just to test it(referred this) , though I am getting the result but as per the second link i mentioned, if the query is same for same parameter value, db hit will take place but on the basis of primary key of relation.

Classes are mentioned below:

Entity Class

@Entity(name="User_Details")
public class UserDetails {
    @Id
    private String userName;
    private String password;
    private String  name;
    private Long msisdn;
   //getter and setter are in place//
}

DAO Class

public class UserDetailsDAOImpl {

SessionFactory sessionFactory;

public UserDetailsDAOImpl() {
    sessionFactory = new Configuration().configure().buildSessionFactory();
}

public UserDetails getUser(String param1) {
    Session session = sessionFactory.openSession();
    System.out.println("session : " + session);     
    session.beginTransaction();        
    Criteria query =  session.createCriteria(UserDetails.class).add(Restrictions.eq("name",param1));
    UserDetails dummy_user=(UserDetails) query.setCacheable(true).uniqueResult();
    session.close();
    return dummy_user;
}

Main Class

    UserDetailsDAOImpl obj=new UserDetailsDAOImpl();        
    UserDetails response = obj.getUser("Borat16");  
    System.out.println("Test:"+response);   
    UserDetails response1 = obj.getUser("Borat16"); 
    System.out.println("Test1:"+response1); 

Hibernate.cfg.xml

    <property name="hibernate.cache.use_second_level_cache">true</property>
    <!--<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>-->
     <property name = "hibernate.cache.use_query_cache">org.hibernate.cache.EhCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>

// First Query Result : As per the article first time it will hit the db (as expected)

Hibernate: select this_.userName as userName1_0_0_, this_.msisdn as msisdn2_0_0_, this_.name as name3_0_0_, this_.password as password4_0_0_ from User_Details this_ where this_.name=?
Test:UserDetails [userName=ak416, password=magnum16, name=Borat16, msisdn=116]

// Second Query Result : Now since the query is same and same parameter why the sql statement generated is not looking like "select * from user_details where userName='ak47'. I mean it should hit db with where parameter as key . What I am missing ?!

Hibernate: select this_.userName as userName1_0_0_, this_.msisdn as msisdn2_0_0_, this_.name as name3_0_0_, this_.password as password4_0_0_ from User_Details this_ where this_.name=?
Test1:UserDetails [userName=ak416, password=magnum16, name=Borat16, msisdn=116]

Upvotes: 0

Views: 75

Answers (2)

Also once you set the cache_provider value ensure the below value is set to "true" to enable query level cache.

<property name="hibernate.cache.use_query_cache">true</property>

Upvotes: 1

jonasnas
jonasnas

Reputation: 3570

You are using

<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

So your cache is not enabled (NoCacheProvider)

Upvotes: 2

Related Questions