Search in sources :

Example 1 with PostLoadEventListener

use of org.hibernate.event.spi.PostLoadEventListener in project hibernate-orm by hibernate.

the class TwoPhaseLoad method postLoad.

/**
 * PostLoad cannot occur during initializeEntity, as that call occurs *before*
 * the Set collections are added to the persistence context by Loader.
 * Without the split, LazyInitializationExceptions can occur in the Entity's
 * postLoad if it acts upon the collection.
 *
 * HHH-6043
 *
 * @param entity The entity
 * @param session The Session
 * @param postLoadEvent The (re-used) post-load event
 */
public static void postLoad(final Object entity, final SharedSessionContractImplementor session, final PostLoadEvent postLoadEvent) {
    if (session.isEventSource()) {
        final PersistenceContext persistenceContext = session.getPersistenceContext();
        final EntityEntry entityEntry = persistenceContext.getEntry(entity);
        postLoadEvent.setEntity(entity).setId(entityEntry.getId()).setPersister(entityEntry.getPersister());
        final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory().getServiceRegistry().getService(EventListenerRegistry.class).getEventListenerGroup(EventType.POST_LOAD);
        for (PostLoadEventListener listener : listenerGroup.listeners()) {
            listener.onPostLoad(postLoadEvent);
        }
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) PostLoadEventListener(org.hibernate.event.spi.PostLoadEventListener) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 2 with PostLoadEventListener

use of org.hibernate.event.spi.PostLoadEventListener in project hibernate-orm by hibernate.

the class DefaultLoadEventListener method assembleCacheEntry.

private Object assembleCacheEntry(final StandardCacheEntryImpl entry, final Serializable id, final EntityPersister persister, final LoadEvent event) throws HibernateException {
    final Object optionalObject = event.getInstanceToLoad();
    final EventSource session = event.getSession();
    final SessionFactoryImplementor factory = session.getFactory();
    if (traceEnabled) {
        LOG.tracev("Assembling entity from second-level cache: {0}", MessageHelper.infoString(persister, id, factory));
    }
    EntityPersister subclassPersister = factory.getEntityPersister(entry.getSubclass());
    Object result = optionalObject == null ? session.instantiate(subclassPersister, id) : optionalObject;
    // make it circular-reference safe
    final EntityKey entityKey = session.generateEntityKey(id, subclassPersister);
    TwoPhaseLoad.addUninitializedCachedEntity(entityKey, result, subclassPersister, LockMode.NONE, entry.getVersion(), session);
    Type[] types = subclassPersister.getPropertyTypes();
    Object[] values = entry.assemble(result, id, subclassPersister, session.getInterceptor(), session);
    // intializes result by side-effect
    TypeHelper.deepCopy(values, types, subclassPersister.getPropertyUpdateability(), values, session);
    Object version = Versioning.getVersion(values, subclassPersister);
    LOG.tracev("Cached Version: {0}", version);
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    boolean isReadOnly = session.isDefaultReadOnly();
    if (persister.isMutable()) {
        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 = true;
    }
    persistenceContext.addEntry(result, (isReadOnly ? Status.READ_ONLY : Status.MANAGED), values, null, id, version, LockMode.NONE, true, subclassPersister, false);
    subclassPersister.afterInitialize(result, session);
    persistenceContext.initializeNonLazyCollections();
    // upgrade the lock if necessary:
    // lock(result, lockMode);
    // PostLoad is needed for EJB3
    // TODO: reuse the PostLoadEvent...
    PostLoadEvent postLoadEvent = event.getPostLoadEvent().setEntity(result).setId(id).setPersister(persister);
    for (PostLoadEventListener listener : postLoadEventListeners(session)) {
        listener.onPostLoad(postLoadEvent);
    }
    return result;
}
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) EntityKey(org.hibernate.engine.spi.EntityKey) 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)

Example 3 with PostLoadEventListener

use of org.hibernate.event.spi.PostLoadEventListener 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)

Aggregations

PersistenceContext (org.hibernate.engine.spi.PersistenceContext)3 PostLoadEventListener (org.hibernate.event.spi.PostLoadEventListener)3 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 EventSource (org.hibernate.event.spi.EventSource)2 EventType (org.hibernate.event.spi.EventType)2 PostLoadEvent (org.hibernate.event.spi.PostLoadEvent)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 HibernateProxy (org.hibernate.proxy.HibernateProxy)2 EmbeddedComponentType (org.hibernate.type.EmbeddedComponentType)2 EntityType (org.hibernate.type.EntityType)2 Type (org.hibernate.type.Type)2 StandardCacheEntryImpl (org.hibernate.cache.spi.entry.StandardCacheEntryImpl)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)1