Example usage for org.hibernate Cache containsEntity

List of usage examples for org.hibernate Cache containsEntity

Introduction

In this page you can find the example usage for org.hibernate Cache containsEntity.

Prototype

boolean containsEntity(String entityName, Serializable identifier);

Source Link

Document

Determine whether the cache contains data for the given entity "instance".

Usage

From source file:com.github.shyiko.rook.target.hibernate.cache.SecondLevelCacheSynchronizerTest.java

License:Apache License

@Test
public void testEvictionOfEntityWithCompositeKey() throws Exception {
    final Cache cache = synchronizationContext.getSessionFactory().getCache();
    final EntityWithCompositeKey firstEntityKey = new EntityWithCompositeKey(1, 2),
            secondEntityKey = new EntityWithCompositeKey(3, 4);
    executeInTransaction(new Callback<Session>() {

        @Override/*  w w  w .jav  a 2 s.c om*/
        public void execute(Session session) {
            session.save(new EntityWithCompositeKey(1, 2, "name_12"));
            session.save(new EntityWithCompositeKey(3, 4, "name_34"));
        }
    });
    executeInTransaction(new Callback<Session>() {

        @Override
        public void execute(Session session) {
            session.get(EntityWithCompositeKey.class, firstEntityKey);
            session.get(EntityWithCompositeKey.class, secondEntityKey);
        }
    });
    executeInTransaction(new Callback<Session>() {

        @Override
        public void execute(Session obj) {
            assertTrue(cache.containsEntity(EntityWithCompositeKey.class, firstEntityKey));
            SecondLevelCacheSynchronizer secondLevelCacheSynchronizer = new SecondLevelCacheSynchronizer(
                    synchronizationContext);
            secondLevelCacheSynchronizer.onEvent(new DeleteRowsReplicationEvent(0, "rook", "entity_with_cpk",
                    new Serializable[] { 2L, 1L }));
            assertFalse(cache.containsEntity(EntityWithCompositeKey.class, firstEntityKey));
            assertTrue(cache.containsEntity(EntityWithCompositeKey.class, secondEntityKey));
        }
    });
}

From source file:com.github.shyiko.rook.target.hibernate.cache.SecondLevelCacheSynchronizerTest.java

License:Apache License

@Test
public void testEvictionOfEntityCollection() throws Exception {
    final Cache cache = synchronizationContext.getSessionFactory().getCache();
    final AtomicLong idHolder = new AtomicLong();

    executeInTransaction(new Callback<Session>() {

        @Override/*  ww  w  . j  ava 2s  .  co m*/
        public void execute(Session session) {
            Entity entity = new Entity();
            entity.setName("Name");

            EntityProperty entityProperty = new EntityProperty();
            entityProperty.setName("name");
            entityProperty.setValue("value");
            entityProperty.setEnclosingEntity(entity);
            entity.getProperties().add(entityProperty);

            idHolder.set((Long) session.save(entity));
        }
    });
    final long ENTITY_ID = idHolder.get();

    executeInTransaction(new Callback<Session>() {

        @Override
        public void execute(Session session) {
            assertNotNull(session.get(Entity.class, ENTITY_ID));
        }
    });

    executeInTransaction(new Callback<Session>() {

        @Override
        public void execute(Session obj) {
            assertTrue(cache.containsEntity(Entity.class, ENTITY_ID));
            SecondLevelCacheSynchronizer secondLevelCacheSynchronizer = new SecondLevelCacheSynchronizer(
                    synchronizationContext);
            secondLevelCacheSynchronizer.onEvent(
                    new DeleteRowsReplicationEvent(0, "rook", "entity", new Serializable[] { ENTITY_ID }));
            assertFalse(cache.containsEntity(Entity.class, ENTITY_ID));

            assertTrue(cache.containsCollection(Entity.class.getName() + ".properties", ENTITY_ID));

            // entity_property table structure [id, name, value, entity_id]
            secondLevelCacheSynchronizer.onEvent(new DeleteRowsReplicationEvent(0, "rook", "entity_property",
                    new Serializable[] { null, null, null, ENTITY_ID }));

            assertFalse(cache.containsCollection(Entity.class.getName() + ".properties", ENTITY_ID));
        }
    });
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.ReadWriteTest.java

License:LGPL

@Test
public void testEntityCacheContentsAfterEvictAll() throws Exception {
    final List<Citizen> citizens = saveSomeCitizens();

    withTxSession(s -> {/*ww  w.j  ava2s.c om*/
        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()); // proxy gets resolved
        assertEquals(1, slcStats.getMissCount());

        // cleanup
        markRollbackOnly(s);
    });
}