Spring 3.1.1.RELEASE、Hibernate 4.1.5.Final、および JUnit 4.8.1 を使用しています。二次キャッシュが適切に使用されているかどうかをテストしたいのですが、正しく使用されているかどうかわかりません。次の JUnit コードは、最後のアサーションで失敗します…</p>
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);
}
これが私のドメイン オブジェクトの構成方法です …</p>
@Entity
@Table(name = "cb_country")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Country implements Serializable
{
…
}
これが私のDAOです…</p>
@Override
public Country findById(String id)
{
Country ret = null;
if (id != null)
{
ret = entityManager.find(Country.class, id);
} // if
return ret;
}
Hibernate の二次キャッシュを構成する方法は次のとおりです。</p>
<!-- 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>
二次キャッシュがヒットを示さない理由はありますか? ありがとう - デイブ