use of org.hibernate.stat.NaturalIdCacheStatistics in project microservices by pwillhan.
the class SecondLevel method cacheNaturalId.
@Test
public void cacheNaturalId() throws Exception {
CacheTestData testData = storeTestData();
Long USER_ID = testData.users.getFirstId();
Long ITEM_ID = testData.items.getFirstId();
UserTransaction tx = TM.getUserTransaction();
try {
Statistics stats = JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics();
// Clear all natural ID cache regions
JPA.getEntityManagerFactory().getCache().unwrap(org.hibernate.Cache.class).evictNaturalIdRegions();
// Clear the User entity cache region
JPA.getEntityManagerFactory().getCache().evict(User.class);
{
tx.begin();
EntityManager em = JPA.createEntityManager();
Session session = em.unwrap(Session.class);
NaturalIdCacheStatistics userIdStats = stats.getNaturalIdCacheStatistics(User.class.getName() + "##NaturalId");
assertEquals(userIdStats.getElementCountInMemory(), 0);
User user = (User) session.byNaturalId(User.class).using("username", "johndoe").load();
// select ID from USERS where USERNAME = ?
// select * from USERS where ID = ?
assertNotNull(user);
assertEquals(userIdStats.getHitCount(), 0);
assertEquals(userIdStats.getMissCount(), 1);
assertEquals(userIdStats.getElementCountInMemory(), 1);
SecondLevelCacheStatistics userStats = stats.getSecondLevelCacheStatistics(User.class.getName());
assertEquals(userStats.getHitCount(), 0);
assertEquals(userStats.getMissCount(), 1);
assertEquals(userStats.getElementCountInMemory(), 1);
tx.commit();
em.close();
}
{
// Execute the lookup again, hit the cache
tx.begin();
EntityManager em = JPA.createEntityManager();
Session session = em.unwrap(Session.class);
/*
The natural identifier cache region for <code>User</code>s
has one element.
*/
NaturalIdCacheStatistics userIdStats = stats.getNaturalIdCacheStatistics(User.class.getName() + "##NaturalId");
assertEquals(userIdStats.getElementCountInMemory(), 1);
/*
The <code>org.hibernate.Session</code> API performs natural
identifier lookup; this is the only API for accessing the
natural identifier cache.
*/
User user = (User) session.byNaturalId(User.class).using("username", "johndoe").load();
assertNotNull(user);
/*
You had a cache hit for the natural identifier lookup; the
cache returned the identifier value of "johndoe".
*/
assertEquals(userIdStats.getHitCount(), 1);
/*
You also had a cache hit for the actual entity data of
that <code>User</code>.
*/
SecondLevelCacheStatistics userStats = stats.getSecondLevelCacheStatistics(User.class.getName());
assertEquals(userStats.getHitCount(), 1);
tx.commit();
em.close();
}
} finally {
TM.rollback();
}
}
use of org.hibernate.stat.NaturalIdCacheStatistics in project hibernate-orm by hibernate.
the class RegionNameTest method testLegacyStatsApi.
// todo (5.3) : any other API I can think of that deals with region-name?
// todo (6.0) : same ^^, maintain API compatibility
@Test
public void testLegacyStatsApi() {
// these references need to be the prefixed name
final String regionName = cachePrefix + '.' + localName;
final Statistics stats = sessionFactory().getStatistics();
assertEquals(2, stats.getSecondLevelCacheRegionNames().length);
final SecondLevelCacheStatistics secondLevelCacheStatistics = stats.getSecondLevelCacheStatistics(regionName);
assert secondLevelCacheStatistics != null;
final NaturalIdCacheStatistics naturalIdCacheStatistics = stats.getNaturalIdCacheStatistics(regionName);
assert naturalIdCacheStatistics != null;
}
use of org.hibernate.stat.NaturalIdCacheStatistics in project hibernate-orm by hibernate.
the class CachedMutableNaturalIdStrictReadWriteTest method testNaturalIdCacheStatisticsReset.
@Test
@TestForIssue(jiraKey = "HHH-9200")
public void testNaturalIdCacheStatisticsReset() {
final String naturalIdCacheRegion = "hibernate.test.org.hibernate.test.naturalid.mutable.cached.Another##NaturalId";
sessionFactory().getStatistics().clear();
Session session = openSession();
session.beginTransaction();
final Another it = new Another("IT");
session.save(it);
session.getTransaction().commit();
session.close();
NaturalIdCacheStatistics statistics = sessionFactory().getStatistics().getNaturalIdCacheStatistics(naturalIdCacheRegion);
assertEquals(1, statistics.getPutCount());
sessionFactory().getStatistics().clear();
// Refresh statistics reference.
statistics = sessionFactory().getStatistics().getNaturalIdCacheStatistics(naturalIdCacheRegion);
assertEquals(0, statistics.getPutCount());
session = openSession();
session.beginTransaction();
session.delete(it);
session.getTransaction().commit();
session.clear();
}
use of org.hibernate.stat.NaturalIdCacheStatistics in project hibernate-orm by hibernate.
the class CachedMutableNaturalIdStrictReadWriteTest method testToMapConversion.
@Test
@TestForIssue(jiraKey = "HHH-9203")
public void testToMapConversion() {
sessionFactory().getStatistics().clear();
final Session session = openSession();
session.getTransaction().begin();
final AllCached it = new AllCached("IT");
session.save(it);
session.getTransaction().commit();
session.close();
final NaturalIdCacheStatistics stats = sessionFactory().getStatistics().getNaturalIdCacheStatistics("hibernate.test." + AllCached.class.getName() + "##NaturalId");
assertEquals(1, stats.getPutCount());
}
Aggregations