title: how to cache an entire table using second level entity caching in hibernate.?
body: <p>I've been banging my head against wall reagrding this issue for over a month.   Please Help..</p>
  
  <p>I want to do second level entity caching (using EHcache) in hibernate for few masters table.i followed <a href="http://ehcache.org/documentation/2.7/integrations/hibernate" rel="nofollow">http://ehcache.org/documentation/2.7/integrations/hibernate</a> and few other links and did the configuration as follows:</p>
  
  <p>for cfg file </p>
  
  <pre><code>    &lt;!-- For Second Level Caching --&gt;
  
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.DB2Dialect&lt;/property&gt;
      &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
       &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.cache.use_query_cache"&gt;false&lt;/property&gt; 
      &lt;property name="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/property&gt;
      &lt;property name = "net.sf.ehcache.configurationResourceName"&gt;/ehcache.xml&lt;/property&gt; 
  
      &lt;!-- For statistics --&gt;
  
      &lt;property name="hibernate.generate_statistics"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.cache.use_structured_entries"&gt;true&lt;/property&gt;
      &lt;property name = "net.sf.hibernate.cache.UpdateTimestampsCache "&gt;true&lt;/property&gt;        
  </code></pre>
  
  <p>hbm file.</p>
  
  <pre><code>    &lt;hibernate-mapping&gt;
          &lt;class name="com.example.ehcacheSample.stateMstr" table="STATE_MSTR"&gt;
  
          &lt;cache include="all" usage="read-only" region = "stateMstr"/&gt;
  
          ...........       
          &lt;/class&gt;
      &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>ehcache.xml</p>
  
  <pre><code>    &lt;cache name = "stateMstr"
          maxElementsInMemory="1"     
          eternal="false"   
          timeToIdleSeconds="300"   
          timeToLiveSeconds="600"
          overflowToDisk= "true" /&gt;
  </code></pre>
  
  <p>now regarding that i have certain issue</p>
  
  <ul>
  <li><p>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 ??</p></li>
  <li><p>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. </p></li>
  </ul>