Search in sources :

Example 1 with Citizen

use of org.hibernate.test.cache.infinispan.functional.entities.Citizen in project hibernate-orm by hibernate.

the class ReadWriteTest method testNaturalIdLoaderCached.

@Test
public void testNaturalIdLoaderCached() throws Exception {
    final Statistics stats = sessionFactory().getStatistics();
    stats.setStatisticsEnabled(true);
    stats.clear();
    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
    assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
    saveSomeCitizens();
    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
    assertEquals("NaturalId Cache Puts", 2, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
    //Try NaturalIdLoadAccess after insert
    final Citizen citizen = withTxSessionApply(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        NaturalIdLoadAccess<Citizen> naturalIdLoader = s.byNaturalId(Citizen.class);
        naturalIdLoader.using("ssn", "1234").using("state", france);
        stats.clear();
        Citizen c = naturalIdLoader.load();
        assertNotNull(c);
        assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
        markRollbackOnly(s);
        return c;
    });
    // TODO: Clear caches manually via cache manager (it's faster!!)
    cleanupCache();
    TIME_SERVICE.advance(1);
    stats.setStatisticsEnabled(true);
    stats.clear();
    //Try NaturalIdLoadAccess
    withTxSession(s -> {
        Citizen loadedCitizen = (Citizen) s.get(Citizen.class, citizen.getId());
        assertNotNull(loadedCitizen);
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
        markRollbackOnly(s);
    });
    // Try NaturalIdLoadAccess after load
    withTxSession(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
        naturalIdLoader.using("ssn", "1234").using("state", france);
        stats.setStatisticsEnabled(true);
        stats.clear();
        Citizen loadedCitizen = (Citizen) naturalIdLoader.load();
        assertNotNull(loadedCitizen);
        assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
        markRollbackOnly(s);
    });
}
Also used : NaturalIdLoadAccess(org.hibernate.NaturalIdLoadAccess) State(org.hibernate.test.cache.infinispan.functional.entities.State) Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 2 with Citizen

use of org.hibernate.test.cache.infinispan.functional.entities.Citizen in project hibernate-orm by hibernate.

the class ReadWriteTest method testEntityCacheContentsAfterEvictAll.

@Test
public void testEntityCacheContentsAfterEvictAll() throws Exception {
    final List<Citizen> citizens = saveSomeCitizens();
    withTxSession(s -> {
        Cache cache = s.getSessionFactory().getCache();
        Statistics stats = sessionFactory().getStatistics();
        SecondLevelCacheStatistics slcStats = stats.getSecondLevelCacheStatistics(Citizen.class.getName());
        assertTrue("2lc entity cache is expected to contain Citizen id = " + citizens.get(0).getId(), cache.containsEntity(Citizen.class, citizens.get(0).getId()));
        assertTrue("2lc entity cache is expected to contain Citizen id = " + citizens.get(1).getId(), cache.containsEntity(Citizen.class, citizens.get(1).getId()));
        assertEquals(2, slcStats.getPutCount());
        cache.evictEntityRegions();
        TIME_SERVICE.advance(1);
        assertEquals(0, slcStats.getElementCountInMemory());
        assertFalse("2lc entity cache is expected to not contain Citizen id = " + citizens.get(0).getId(), cache.containsEntity(Citizen.class, citizens.get(0).getId()));
        assertFalse("2lc entity cache is expected to not contain Citizen id = " + citizens.get(1).getId(), cache.containsEntity(Citizen.class, citizens.get(1).getId()));
        Citizen citizen = s.load(Citizen.class, citizens.get(0).getId());
        assertNotNull(citizen);
        assertNotNull(citizen.getFirstname());
        assertEquals(1, slcStats.getMissCount());
        markRollbackOnly(s);
    });
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Cache(org.hibernate.Cache) Test(org.junit.Test)

Example 3 with Citizen

use of org.hibernate.test.cache.infinispan.functional.entities.Citizen in project hibernate-orm by hibernate.

the class ReadWriteTest method testMultipleEvictAll.

@Test
public void testMultipleEvictAll() throws Exception {
    final List<Citizen> citizens = saveSomeCitizens();
    withTxSession(s -> {
        Cache cache = s.getSessionFactory().getCache();
        cache.evictEntityRegions();
        cache.evictEntityRegions();
    });
    withTxSession(s -> {
        Cache cache = s.getSessionFactory().getCache();
        cache.evictEntityRegions();
        s.delete(s.load(Citizen.class, citizens.get(0).getId()));
        s.delete(s.load(Citizen.class, citizens.get(1).getId()));
    });
}
Also used : Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Cache(org.hibernate.Cache) Test(org.junit.Test)

Example 4 with Citizen

use of org.hibernate.test.cache.infinispan.functional.entities.Citizen in project hibernate-orm by hibernate.

the class NaturalIdInvalidationTest method saveSomeCitizens.

private void saveSomeCitizens(SessionFactory sf) throws Exception {
    final Citizen c1 = new Citizen();
    c1.setFirstname("Emmanuel");
    c1.setLastname("Bernard");
    c1.setSsn("1234");
    final State france = new State();
    france.setName("Ile de France");
    c1.setState(france);
    final Citizen c2 = new Citizen();
    c2.setFirstname("Gavin");
    c2.setLastname("King");
    c2.setSsn("000");
    final State australia = new State();
    australia.setName("Australia");
    c2.setState(australia);
    withTxSession(sf, s -> {
        s.persist(australia);
        s.persist(france);
        s.persist(c1);
        s.persist(c2);
    });
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen)

Example 5 with Citizen

use of org.hibernate.test.cache.infinispan.functional.entities.Citizen in project hibernate-orm by hibernate.

the class NaturalIdInvalidationTest method deleteCitizenWithCriteria.

private void deleteCitizenWithCriteria(SessionFactory sf) throws Exception {
    withTxSession(sf, s -> {
        State france = getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);
        Citizen c = (Citizen) criteria.uniqueResult();
        s.delete(c);
    });
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Criteria(org.hibernate.Criteria)

Aggregations

Citizen (org.hibernate.test.cache.infinispan.functional.entities.Citizen)6 State (org.hibernate.test.cache.infinispan.functional.entities.State)4 Test (org.junit.Test)3 Cache (org.hibernate.Cache)2 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)2 Statistics (org.hibernate.stat.Statistics)2 ArrayList (java.util.ArrayList)1 Criteria (org.hibernate.Criteria)1 NaturalIdLoadAccess (org.hibernate.NaturalIdLoadAccess)1