Search in sources :

Example 1 with EntityRegionImpl

use of org.hibernate.testing.cache.EntityRegionImpl in project hibernate-orm by hibernate.

the class BasicStructuredCachingOfConvertedValueTest method basicCacheStructureTest.

@Test
@TestForIssue(jiraKey = "HHH-9615")
@SuppressWarnings("unchecked")
public void basicCacheStructureTest() {
    EntityPersister persister = sessionFactory().getMetamodel().entityPersisters().get(Address.class.getName());
    EntityRegionImpl region = (EntityRegionImpl) 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();
    {
        final Object cachedItem = region.getDataMap().values().iterator().next();
        final Map<String, ?> state = (Map) ((ReadWriteEntityRegionAccessStrategy.Item) cachedItem).getValue();
        // this is the point of the Jira.. that this "should be" the converted value
        assertThat(state.get("postalArea"), instanceOf(PostalArea.class));
    }
    assertThat(PostalAreaConverter.toDatabaseCallCount, is(1));
    assertThat(PostalAreaConverter.toDomainCallCount, is(0));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // test during load...
    PostalAreaConverter.clearCounts();
    region.evictAll();
    session = openSession();
    session.getTransaction().begin();
    Address address = session.get(Address.class, 1);
    session.getTransaction().commit();
    session.close();
    {
        final Object cachedItem = region.getDataMap().values().iterator().next();
        final Map<String, ?> state = (Map) ((ReadWriteEntityRegionAccessStrategy.Item) cachedItem).getValue();
        // this is the point of the Jira.. that this "should be" the converted value
        assertThat(state.get("postalArea"), 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) EntityRegionImpl(org.hibernate.testing.cache.EntityRegionImpl) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with EntityRegionImpl

use of org.hibernate.testing.cache.EntityRegionImpl 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());
    EntityRegionImpl region = (EntityRegionImpl) 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();
    {
        final Object cachedItem = region.getDataMap().values().iterator().next();
        final StandardCacheEntryImpl state = (StandardCacheEntryImpl) ((ReadWriteEntityRegionAccessStrategy.Item) cachedItem).getValue();
        assertThat(state.getDisassembledState()[postalAreaAttributeIndex], instanceOf(PostalArea.class));
    }
    assertThat(PostalAreaConverter.toDatabaseCallCount, is(1));
    assertThat(PostalAreaConverter.toDomainCallCount, is(0));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // test during load...
    PostalAreaConverter.clearCounts();
    region.evictAll();
    session = openSession();
    session.getTransaction().begin();
    Address address = session.get(Address.class, 1);
    session.getTransaction().commit();
    session.close();
    {
        final Object cachedItem = region.getDataMap().values().iterator().next();
        final StandardCacheEntryImpl state = (StandardCacheEntryImpl) ((ReadWriteEntityRegionAccessStrategy.Item) cachedItem).getValue();
        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) EntityRegionImpl(org.hibernate.testing.cache.EntityRegionImpl) StandardCacheEntryImpl(org.hibernate.cache.spi.entry.StandardCacheEntryImpl) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

Session (org.hibernate.Session)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 TestForIssue (org.hibernate.testing.TestForIssue)2 EntityRegionImpl (org.hibernate.testing.cache.EntityRegionImpl)2 Test (org.junit.Test)2 Map (java.util.Map)1 StandardCacheEntryImpl (org.hibernate.cache.spi.entry.StandardCacheEntryImpl)1