3

I've been banging my head against wall reagrding this issue for over a month. Please Help..

I want to do second level entity caching (using EHcache) in hibernate for few masters table.i followed http://ehcache.org/documentation/2.7/integrations/hibernate and few other links and did the configuration as follows:

for cfg file

    <!-- For Second Level Caching -->

    <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
    <property name="hibernate.show_sql">true</property>
     <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.use_query_cache">false</property> 
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    <property name = "net.sf.ehcache.configurationResourceName">/ehcache.xml</property> 

    <!-- For statistics -->

    <property name="hibernate.generate_statistics">true</property>
    <property name="hibernate.cache.use_structured_entries">true</property>
    <property name = "net.sf.hibernate.cache.UpdateTimestampsCache ">true</property>        

hbm file.

    <hibernate-mapping>
        <class name="com.example.ehcacheSample.stateMstr" table="STATE_MSTR">

        <cache include="all" usage="read-only" region = "stateMstr"/>

        ...........       
        </class>
    </hibernate-mapping>

ehcache.xml

    <cache name = "stateMstr"
        maxElementsInMemory="1"     
        eternal="false"   
        timeToIdleSeconds="300"   
        timeToLiveSeconds="600"
        overflowToDisk= "true" />

now regarding that i have certain issue

  • the document says that for second level caching, entity needs to be loaded using primary key only. which means that for getting a table of 100 rows in the cache we have to run a loop 100 times using the primary key of the columns as the serializable id. In other words if I would like to create a replica of the table in the cache, Is loading an entity row by row an only option ??

  • secondly if we want to fetch the primary Id of a row using a given value of any column of the same row from the cache ,is that possible without hitting a database query ? For eg if the deptt name is known and i would like to fetch the id of the deptt and deptt table is cached .. is that possible to get the id without hitting a database query.

Anushree
  • 41
  • 1
  • 4

1 Answers1

0

I had some issues like you

first, if you want to load all the rows of a table, you can use the query cache (change in your hibernate configuration to true)

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

in your query you have to use the setCacheable(true) method to tell hibernate to save the result of the query.

session.createQuery("from deptt").setCacheable(true);

now you have a query and the result of the query in your query cache (the result is a list of your Depp (all the table)),

if you want to use this List you have got to use the same query to get the list of your Depp again without going to data base.

session.createQuery("from deptt").setCacheable(true);

you put the result into a Set, List, ... if you want to get a single Depp, you can have it from the list of Depp that you have.

Bilal BBB
  • 1,154
  • 13
  • 20
  • sorry ... setCacheable(true) and not setQuery(true) – Bilal BBB Feb 25 '15 at 13:40
  • Thanks Bill for ur reply. I tried doing that as well, but the only reason i moved to entity caching is that i read in many links that entity caching is better than query caching. Secondly i even saw that whenever query caching is executed a new folder is made at the external location, while in entity caching single file is replaced evrytime it gets executed. – Anushree Feb 26 '15 at 04:32
  • Query Caching should be avoided as it will bloat the size of the cache quickly – Sumit Kumar Saha May 28 '19 at 12:55