I'm using Spring 3.1.1.RELEASE, Hibernate 4.1.5.Final, and JUnit 4.8.1. I want to test if my second-level cache is being properly used, but unsure if I'm doing it right. The following JUnit code fails on the last assertion …
private Statistics m_stats;
@Before
public void setup()
{
m_stats = ((org.hibernate.internal.SessionImpl) m_entityManager.getDelegate()).getSessionFactory().getStatistics();
} // setup
@Test
public void testGenerateStats()
{
final String countryId = m_testProps.getProperty("test.country.id");
final Country country = m_countryDao.findById(countryId);
Assert.assertNotNull(country);
final Country country2 = m_countryDao.findById(countryId);
m_countryDao.findById(countryId);
Assert.assertTrue(m_stats.getSecondLevelCacheHitCount() > 0);
}
Here is how I configured my domain object …
@Entity
@Table(name = "cb_country")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Country implements Serializable
{
…
}
and here's my DAO …
@Override
public Country findById(String id)
{
Country ret = null;
if (id != null)
{
ret = entityManager.find(Country.class, id);
} // if
return ret;
}
Here's how I configured my Hibernate second level cache …
<!-- Caching -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- Collect stats, this is for testing if the cache is working -->
<property name="hibernate.generate_statistics">true</property>
Any ideas why my second level cache is not indicating any hits? Thanks - Dave