import org.hibernate.Session;
import org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.test.jpa.AbstractJPATest;
import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
public class NaturalIdCacheTest extends AbstractJPATest {
@Override
public String[] getMappings() {
return NO_MAPPINGS;
}
@Override
public void configure(Configuration cfg) {
super.configure(cfg);
cfg.addAnnotatedClass(EntityWithNaturalKey.class);
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.CACHE_REGION_FACTORY, SingletonEhCacheRegionFactory.class.getName());
}
@Test
@TestForIssue(jiraKey = "???")
public void testPutIntoNaturalIdCache_newlyCreatedIdentity() throws Exception {
Session s = openSession();
s.beginTransaction();
EntityWithNaturalKey e = new EntityWithNaturalKey("test");
s.persist(e);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
e = (EntityWithNaturalKey) s.bySimpleNaturalId(EntityWithNaturalKey.class).load("test");
Assert.assertNotNull(e);
s.getTransaction().commit();
s.close();
long queryCount = sessionFactory().getStatistics().getNaturalIdQueryExecutionCount();
s = openSession();
s.beginTransaction();
e = (EntityWithNaturalKey) s.bySimpleNaturalId(EntityWithNaturalKey.class).load("test");
Assert.assertNotNull(e);
s.getTransaction().commit();
s.close();
Assert.assertEquals(queryCount, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount());
}
}