0

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]
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
balboa_21
  • 375
  • 1
  • 7
  • 21

2 Answers2

2

You are using

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

So your cache is not enabled (NoCacheProvider)

jonasnas
  • 3,540
  • 1
  • 23
  • 32
  • Thank You for pointing this out...I copy-paste the old config from some project. Can you tell me what class to use..because I am not able to find it..and even after commenting it out I am getting the same result !! – balboa_21 Dec 05 '15 at 22:57
  • 1
    You don't want to comment out. You need to reference some class which implements caching. You can use 'org.hibernate.cache.EhCacheProvider'. There are many tutorials about it. For example: http://howtodoinjava.com/2013/07/04/hibernate-ehcache-configuration-tutorial/ You need to provide the cache provider dependency in your pom.xml file, and reference implementation class in configuration. – jonasnas Dec 05 '15 at 23:52
  • Thanks alot I will do that asap...anyways..I forgot to mention that I also added (which was mentioned in second post) this property but still I am getting the same result `org.hibernate.cache.EhCacheProvider` ...so in short do I still have to configure `cache.provider_class` ? – balboa_21 Dec 06 '15 at 08:50
  • use_query_cache is a boolean not a class. So yes you need to set provider_class. Better follow some complete tutorial, have something working and start customising from there. – jonasnas Dec 06 '15 at 10:42
  • thanks alot...the main confusion was this only..because this [post](http://stackoverflow.com/questions/19516451/hibernate-first-level-cache-vs-query-cache) mentioned it as class and boolean both.. – balboa_21 Dec 06 '15 at 11:18
1

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>
  • Thats confusing me...as per second [post](http://stackoverflow.com/questions/19516451/hibernate-first-level-cache-vs-query-cache)..!!..the same property has two values set. `hibernate.cache.use_query_cache=org.hibernate.cache.EhCacheProvider hibernate.cache.use_query_cache=true` – balboa_21 Dec 06 '15 at 08:58
  • The property "hibernate.cache.use_query_cache" takes a boolean value. Looks like you have set cacheProvider value to this property by mistake. So you need to replace current value of "org.hibernate.cache.EhCacheProvider" with "true" for this property "hibernate.cache.use_query_cache". – Madhusudana Reddy Sunnapu Dec 06 '15 at 15:24
  • By the way, in the latest versions of hibernate the property "hibernate.cache.provider_class" is replaced by "hibernate.cache.region.factory_class". This is how it looks with region factory and use_query_cache property - org.hibernate.cache.ehcache.EhCacheRegionFactory true true – Madhusudana Reddy Sunnapu Dec 06 '15 at 15:33