Search in sources :

Example 1 with PreLoadEvent

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

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

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

the class StandardCacheEntryImpl method assemble.

/**
	 * Assemble the previously disassembled state represented by this entry into the given entity instance.
	 *
	 * Additionally manages the PreLoadEvent callbacks.
	 *
	 * @param instance The entity instance
	 * @param id The entity identifier
	 * @param persister The entity persister
	 * @param interceptor (currently unused)
	 * @param session The session
	 *
	 * @return The assembled state
	 *
	 * @throws HibernateException Indicates a problem performing assembly or calling the PreLoadEventListeners.
	 *
	 * @see org.hibernate.type.Type#assemble
	 * @see org.hibernate.type.Type#disassemble
	 */
public Object[] assemble(final Object instance, final Serializable id, final EntityPersister persister, final Interceptor interceptor, final EventSource session) throws HibernateException {
    if (!persister.getEntityName().equals(subclass)) {
        throw new AssertionFailure("Tried to assemble a different subclass instance");
    }
    //assembled state gets put in a new array (we read from cache by value!)
    final Object[] assembledProps = TypeHelper.assemble(disassembledState, persister.getPropertyTypes(), session, instance);
    //persister.setIdentifier(instance, id); //beforeQuery calling interceptor, for consistency with normal load
    //TODO: reuse the PreLoadEvent
    final PreLoadEvent preLoadEvent = new PreLoadEvent(session).setEntity(instance).setState(assembledProps).setId(id).setPersister(persister);
    final EventListenerGroup<PreLoadEventListener> listenerGroup = session.getFactory().getServiceRegistry().getService(EventListenerRegistry.class).getEventListenerGroup(EventType.PRE_LOAD);
    for (PreLoadEventListener listener : listenerGroup.listeners()) {
        listener.onPreLoad(preLoadEvent);
    }
    persister.setPropertyValues(instance, assembledProps);
    return assembledProps;
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) PreLoadEventListener(org.hibernate.event.spi.PreLoadEventListener) PreLoadEvent(org.hibernate.event.spi.PreLoadEvent) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 4 with PreLoadEvent

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

PreLoadEvent (org.hibernate.event.spi.PreLoadEvent)4 EventSource (org.hibernate.event.spi.EventSource)3 PostLoadEvent (org.hibernate.event.spi.PostLoadEvent)3 AssertionFailure (org.hibernate.AssertionFailure)1 HibernateException (org.hibernate.HibernateException)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)1 PreLoadEventListener (org.hibernate.event.spi.PreLoadEventListener)1 AfterLoadAction (org.hibernate.loader.spi.AfterLoadAction)1 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)1