Search in sources :

Example 6 with DomainDataRegion

use of org.hibernate.cache.spi.DomainDataRegion in project hibernate-orm by hibernate.

the class BasicUnstructuredCachingOfConvertedValueTest method basicCacheStructureTest.

@Test
@TestForIssue(jiraKey = "HHH-9615")
@SuppressWarnings("unchecked")
public void basicCacheStructureTest() {
    EntityPersister persister = sessionFactory().getMetamodel().entityPersisters().get(Address.class.getName());
    final DomainDataRegion region = persister.getCacheAccessStrategy().getRegion();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // test during store...
    PostalAreaConverter.clearCounts();
    Session session = openSession();
    session.getTransaction().begin();
    session.save(new Address(1, "123 Main St.", null, PostalArea._78729));
    session.getTransaction().commit();
    session.close();
    {
        inSession(s -> {
            final EntityDataAccess entityDataAccess = region.getEntityDataAccess(persister.getNavigableRole());
            final Object cacheKey = entityDataAccess.generateCacheKey(1, persister, sessionFactory(), null);
            final Object cachedItem = entityDataAccess.get(s, cacheKey);
            final StandardCacheEntryImpl state = (StandardCacheEntryImpl) cachedItem;
            // this is the point of the Jira.. that this "should be" the converted value
            assertThat(state.getDisassembledState()[postalAreaAttributeIndex], instanceOf(PostalArea.class));
        });
    }
    assertThat(PostalAreaConverter.toDatabaseCallCount, is(1));
    assertThat(PostalAreaConverter.toDomainCallCount, is(0));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // test during load...
    PostalAreaConverter.clearCounts();
    sessionFactory().getCache().evictAll();
    session = openSession();
    session.getTransaction().begin();
    Address address = session.get(Address.class, 1);
    session.getTransaction().commit();
    session.close();
    {
        inSession(s -> {
            final EntityDataAccess entityDataAccess = region.getEntityDataAccess(persister.getNavigableRole());
            final Object cacheKey = entityDataAccess.generateCacheKey(1, persister, sessionFactory(), null);
            final Object cachedItem = entityDataAccess.get(s, cacheKey);
            final StandardCacheEntryImpl state = (StandardCacheEntryImpl) cachedItem;
            // this is the point of the Jira.. that this "should be" the converted value
            assertThat(state.getDisassembledState()[postalAreaAttributeIndex], instanceOf(PostalArea.class));
        });
    }
    assertThat(PostalAreaConverter.toDatabaseCallCount, is(0));
    assertThat(PostalAreaConverter.toDomainCallCount, is(1));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // cleanup
    session = openSession();
    session.getTransaction().begin();
    session.delete(address);
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CoreMatchers.is(org.hamcrest.CoreMatchers.is) EntityPersister(org.hibernate.persister.entity.EntityPersister) BaseNonConfigCoreFunctionalTestCase(org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase) AvailableSettings(org.hibernate.cfg.AvailableSettings) CachingRegionFactory(org.hibernate.testing.cache.CachingRegionFactory) Session(org.hibernate.Session) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) Test(org.junit.Test) StandardCacheEntryImpl(org.hibernate.cache.spi.entry.StandardCacheEntryImpl) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) TestForIssue(org.hibernate.testing.TestForIssue) Map(java.util.Map) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) StandardCacheEntryImpl(org.hibernate.cache.spi.entry.StandardCacheEntryImpl) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) Session(org.hibernate.Session) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

DomainDataRegion (org.hibernate.cache.spi.DomainDataRegion)6 NaturalIdDataAccess (org.hibernate.cache.spi.access.NaturalIdDataAccess)4 ExtendedStatisticsSupport (org.hibernate.cache.spi.ExtendedStatisticsSupport)3 Region (org.hibernate.cache.spi.Region)3 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)3 Map (java.util.Map)2 CoreMatchers.instanceOf (org.hamcrest.CoreMatchers.instanceOf)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Session (org.hibernate.Session)2 AvailableSettings (org.hibernate.cfg.AvailableSettings)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 TestForIssue (org.hibernate.testing.TestForIssue)2 CachingRegionFactory (org.hibernate.testing.cache.CachingRegionFactory)2 BaseNonConfigCoreFunctionalTestCase (org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 HibernateException (org.hibernate.HibernateException)1 CollectionDataCachingConfig (org.hibernate.cache.cfg.spi.CollectionDataCachingConfig)1