Search in sources :

Example 1 with PostLoadEvent

use of org.hibernate.event.spi.PostLoadEvent 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 2 with PostLoadEvent

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

the class AbstractRowReader method finishUp.

@Override
public void finishUp(ResultSetProcessingContextImpl context, List<AfterLoadAction> afterLoadActionList) {
    final List<HydratedEntityRegistration> hydratedEntityRegistrations = context.getHydratedEntityRegistrationList();
    // for arrays, we should end the collection load beforeQuery resolving the entities, since the
    // actual array instances are not instantiated during loading
    finishLoadingArrays(context);
    // IMPORTANT: reuse the same event instances for performance!
    final PreLoadEvent preLoadEvent;
    final PostLoadEvent postLoadEvent;
    if (context.getSession().isEventSource()) {
        preLoadEvent = new PreLoadEvent((EventSource) context.getSession());
        postLoadEvent = new PostLoadEvent((EventSource) context.getSession());
    } else {
        preLoadEvent = null;
        postLoadEvent = null;
    }
    // now finish loading the entities (2-phase load)
    performTwoPhaseLoad(preLoadEvent, context, hydratedEntityRegistrations);
    // now we can finalize loading collections
    finishLoadingCollections(context);
    // finally, perform post-load operations
    postLoad(postLoadEvent, context, hydratedEntityRegistrations, afterLoadActionList);
}
Also used : PostLoadEvent(org.hibernate.event.spi.PostLoadEvent) EventSource(org.hibernate.event.spi.EventSource) PreLoadEvent(org.hibernate.event.spi.PreLoadEvent)

Example 3 with PostLoadEvent

use of org.hibernate.event.spi.PostLoadEvent 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 4 with PostLoadEvent

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

the class Loader method initializeEntitiesAndCollections.

private void initializeEntitiesAndCollections(final List hydratedObjects, final Object resultSetId, final SharedSessionContractImplementor session, final boolean readOnly, List<AfterLoadAction> afterLoadActions) throws HibernateException {
    final CollectionPersister[] collectionPersisters = getCollectionPersisters();
    if (collectionPersisters != null) {
        for (CollectionPersister collectionPersister : collectionPersisters) {
            if (collectionPersister.isArray()) {
                //for arrays, we should end the collection load beforeQuery resolving
                //the entities, since the actual array instances are not instantiated
                //during loading
                //TODO: or we could do this polymorphically, and have two
                //      different operations implemented differently for arrays
                endCollectionLoad(resultSetId, session, collectionPersister);
            }
        }
    }
    //important: reuse the same event instances for performance!
    final PreLoadEvent pre;
    final PostLoadEvent post;
    if (session.isEventSource()) {
        pre = new PreLoadEvent((EventSource) session);
        post = new PostLoadEvent((EventSource) session);
    } else {
        pre = null;
        post = null;
    }
    if (hydratedObjects != null) {
        int hydratedObjectsSize = hydratedObjects.size();
        LOG.tracev("Total objects hydrated: {0}", hydratedObjectsSize);
        for (Object hydratedObject : hydratedObjects) {
            TwoPhaseLoad.initializeEntity(hydratedObject, readOnly, session, pre);
        }
    }
    if (collectionPersisters != null) {
        for (CollectionPersister collectionPersister : collectionPersisters) {
            if (!collectionPersister.isArray()) {
                //for sets, we should end the collection load afterQuery resolving
                //the entities, since we might call hashCode() on the elements
                //TODO: or we could do this polymorphically, and have two
                //      different operations implemented differently for arrays
                endCollectionLoad(resultSetId, session, collectionPersister);
            }
        }
    }
    // persistence context.
    if (hydratedObjects != null) {
        for (Object hydratedObject : hydratedObjects) {
            TwoPhaseLoad.postLoad(hydratedObject, session, post);
            if (afterLoadActions != null) {
                for (AfterLoadAction afterLoadAction : afterLoadActions) {
                    final EntityEntry entityEntry = session.getPersistenceContext().getEntry(hydratedObject);
                    if (entityEntry == null) {
                        // big problem
                        throw new HibernateException("Could not locate EntityEntry immediately afterQuery two-phase load");
                    }
                    afterLoadAction.afterLoad(session, hydratedObject, (Loadable) entityEntry.getPersister());
                }
            }
        }
    }
}
Also used : PostLoadEvent(org.hibernate.event.spi.PostLoadEvent) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) HibernateException(org.hibernate.HibernateException) AfterLoadAction(org.hibernate.loader.spi.AfterLoadAction) PreLoadEvent(org.hibernate.event.spi.PreLoadEvent)

Example 5 with PostLoadEvent

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

the class CustomPersister method load.

/**
	 * @see EntityPersister#load(Serializable, Object, LockMode, SharedSessionContractImplementor)
	 */
public Object load(Serializable id, Object optionalObject, LockMode lockMode, SharedSessionContractImplementor session) throws HibernateException {
    // fails when optional object is supplied
    Custom clone = null;
    Custom obj = (Custom) INSTANCES.get(id);
    if (obj != null) {
        clone = (Custom) obj.clone();
        TwoPhaseLoad.addUninitializedEntity(session.generateEntityKey(id, this), clone, this, LockMode.NONE, session);
        TwoPhaseLoad.postHydrate(this, id, new String[] { obj.getName() }, null, clone, LockMode.NONE, session);
        TwoPhaseLoad.initializeEntity(clone, false, session, new PreLoadEvent((EventSource) session));
        TwoPhaseLoad.postLoad(clone, session, new PostLoadEvent((EventSource) session));
    }
    return clone;
}
Also used : PostLoadEvent(org.hibernate.event.spi.PostLoadEvent) EventSource(org.hibernate.event.spi.EventSource) PreLoadEvent(org.hibernate.event.spi.PreLoadEvent)

Aggregations

EventSource (org.hibernate.event.spi.EventSource)5 PostLoadEvent (org.hibernate.event.spi.PostLoadEvent)5 PreLoadEvent (org.hibernate.event.spi.PreLoadEvent)3 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)2 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 EventType (org.hibernate.event.spi.EventType)2 PostLoadEventListener (org.hibernate.event.spi.PostLoadEventListener)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 HibernateException (org.hibernate.HibernateException)1 StandardCacheEntryImpl (org.hibernate.cache.spi.entry.StandardCacheEntryImpl)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 AfterLoadAction (org.hibernate.loader.spi.AfterLoadAction)1 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)1