Search in sources :

Example 1 with StandardCacheEntryImpl

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

the class DefaultLoadEventListener method convertCacheEntryToEntity.

private Object convertCacheEntryToEntity(CacheEntry entry, Serializable entityId, EntityPersister persister, LoadEvent event, EntityKey entityKey) {
    final EventSource session = event.getSession();
    final SessionFactoryImplementor factory = session.getFactory();
    final EntityPersister subclassPersister;
    if (traceEnabled) {
        LOG.tracef("Converting second-level cache entry [%s] into entity : %s", entry, MessageHelper.infoString(persister, entityId, factory));
    }
    final Object entity;
    subclassPersister = factory.getEntityPersister(entry.getSubclass());
    final Object optionalObject = event.getInstanceToLoad();
    entity = optionalObject == null ? session.instantiate(subclassPersister, entityId) : optionalObject;
    // make it circular-reference safe
    TwoPhaseLoad.addUninitializedCachedEntity(entityKey, entity, subclassPersister, LockMode.NONE, entry.getVersion(), session);
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final Object[] values;
    final Object version;
    final boolean isReadOnly;
    final Type[] types = subclassPersister.getPropertyTypes();
    // initializes the entity by (desired) side-effect
    values = ((StandardCacheEntryImpl) entry).assemble(entity, entityId, subclassPersister, session.getInterceptor(), session);
    if (((StandardCacheEntryImpl) entry).isDeepCopyNeeded()) {
        TypeHelper.deepCopy(values, types, subclassPersister.getPropertyUpdateability(), values, session);
    }
    version = Versioning.getVersion(values, subclassPersister);
    LOG.tracef("Cached Version : %s", version);
    final Object proxy = persistenceContext.getProxy(entityKey);
    if (proxy != null) {
        // there is already a proxy for this impl
        // only set the status to read-only if the proxy is read-only
        isReadOnly = ((HibernateProxy) proxy).getHibernateLazyInitializer().isReadOnly();
    } else {
        isReadOnly = session.isDefaultReadOnly();
    }
    persistenceContext.addEntry(entity, (isReadOnly ? Status.READ_ONLY : Status.MANAGED), values, null, entityId, version, LockMode.NONE, true, subclassPersister, false);
    subclassPersister.afterInitialize(entity, session);
    persistenceContext.initializeNonLazyCollections();
    //PostLoad is needed for EJB3
    PostLoadEvent postLoadEvent = event.getPostLoadEvent().setEntity(entity).setId(entityId).setPersister(persister);
    for (PostLoadEventListener listener : postLoadEventListeners(session)) {
        listener.onPostLoad(postLoadEvent);
    }
    return entity;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) PostLoadEvent(org.hibernate.event.spi.PostLoadEvent) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) StatefulPersistenceContext(org.hibernate.engine.internal.StatefulPersistenceContext) HibernateProxy(org.hibernate.proxy.HibernateProxy) PostLoadEventListener(org.hibernate.event.spi.PostLoadEventListener) EventSource(org.hibernate.event.spi.EventSource) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) EventType(org.hibernate.event.spi.EventType) Type(org.hibernate.type.Type) StandardCacheEntryImpl(org.hibernate.cache.spi.entry.StandardCacheEntryImpl)

Example 2 with StandardCacheEntryImpl

use of org.hibernate.cache.spi.entry.StandardCacheEntryImpl 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)

Example 3 with StandardCacheEntryImpl

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

the class InitFromCacheTestTask method execute.

@Override
public void execute() {
    EntityPersister p = sessionFactory().getEntityPersister(Document.class.getName());
    assertTrue(p.hasCache());
    BaseRegion region = (BaseRegion) p.getCacheAccessStrategy().getRegion();
    Session s = sessionFactory().openSession();
    s.beginTransaction();
    s.persist(new Document("HiA", "Hibernate book", "Hibernate is...."));
    s.getTransaction().commit();
    s.close();
    s = sessionFactory().openSession();
    s.beginTransaction();
    Document d = (Document) s.createQuery("from Document fetch all properties").uniqueResult();
    assertTrue(Hibernate.isPropertyInitialized(d, "text"));
    assertTrue(Hibernate.isPropertyInitialized(d, "summary"));
    s.getTransaction().commit();
    s.close();
    StandardCacheEntryImpl cacheEntry = (StandardCacheEntryImpl) region.getDataMap().get(p.getCacheAccessStrategy().generateCacheKey(d.getId(), p, sessionFactory(), null));
    assertNotNull(cacheEntry);
    sessionFactory().getStatistics().clear();
    s = sessionFactory().openSession();
    s.beginTransaction();
    d = (Document) s.createCriteria(Document.class).uniqueResult();
    assertFalse(Hibernate.isPropertyInitialized(d, "text"));
    assertFalse(Hibernate.isPropertyInitialized(d, "summary"));
    assertEquals("Hibernate is....", d.getText());
    assertTrue(Hibernate.isPropertyInitialized(d, "text"));
    assertTrue(Hibernate.isPropertyInitialized(d, "summary"));
    s.getTransaction().commit();
    s.close();
    assertEquals(2, sessionFactory().getStatistics().getPrepareStatementCount());
    s = sessionFactory().openSession();
    s.beginTransaction();
    d = s.get(Document.class, d.getId());
    assertFalse(Hibernate.isPropertyInitialized(d, "text"));
    assertFalse(Hibernate.isPropertyInitialized(d, "summary"));
    s.getTransaction().commit();
    s.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BaseRegion(org.hibernate.testing.cache.BaseRegion) StandardCacheEntryImpl(org.hibernate.cache.spi.entry.StandardCacheEntryImpl) Session(org.hibernate.Session)

Aggregations

StandardCacheEntryImpl (org.hibernate.cache.spi.entry.StandardCacheEntryImpl)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 Session (org.hibernate.Session)2 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 EventSource (org.hibernate.event.spi.EventSource)1 EventType (org.hibernate.event.spi.EventType)1 PostLoadEvent (org.hibernate.event.spi.PostLoadEvent)1 PostLoadEventListener (org.hibernate.event.spi.PostLoadEventListener)1 HibernateProxy (org.hibernate.proxy.HibernateProxy)1 TestForIssue (org.hibernate.testing.TestForIssue)1 BaseRegion (org.hibernate.testing.cache.BaseRegion)1 EntityRegionImpl (org.hibernate.testing.cache.EntityRegionImpl)1 EmbeddedComponentType (org.hibernate.type.EmbeddedComponentType)1 EntityType (org.hibernate.type.EntityType)1 Type (org.hibernate.type.Type)1 Test (org.junit.Test)1