Search in sources :

Example 31 with EventSource

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

the class DefaultSaveOrUpdateEventListener method performUpdate.

protected void performUpdate(SaveOrUpdateEvent event, Object entity, EntityPersister persister) throws HibernateException {
    final boolean traceEnabled = LOG.isTraceEnabled();
    if (traceEnabled && !persister.isMutable()) {
        LOG.trace("Immutable instance passed to performUpdate()");
    }
    if (traceEnabled) {
        LOG.tracev("Updating {0}", MessageHelper.infoString(persister, event.getRequestedId(), event.getSession().getFactory()));
    }
    final EventSource source = event.getSession();
    final EntityKey key = source.generateEntityKey(event.getRequestedId(), persister);
    source.getPersistenceContext().checkUniqueness(key, entity);
    if (invokeUpdateLifecycle(entity, persister, source)) {
        reassociate(event, event.getObject(), event.getRequestedId(), persister);
        return;
    }
    // this is a transient object with existing persistent state not loaded by the session
    new OnUpdateVisitor(source, event.getRequestedId(), entity).process(entity, persister);
    // TODO: put this stuff back in to read snapshot from
    // the second-level cache (needs some extra work)
    /*Object[] cachedState = null;

        if ( persister.hasCache() ) {
        	CacheEntry entry = (CacheEntry) persister.getCache()
        			.get( event.getRequestedId(), source.getTimestamp() );
            cachedState = entry==null ?
            		null :
            		entry.getState(); //TODO: half-assemble this stuff
        }*/
    source.getPersistenceContext().addEntity(entity, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), // cachedState,
    null, key, persister.getVersion(entity), LockMode.NONE, true, persister, false);
    persister.afterReassociate(entity, source);
    if (traceEnabled) {
        LOG.tracev("Updating {0}", MessageHelper.infoString(persister, event.getRequestedId(), source.getFactory()));
    }
    cascadeOnUpdate(event, persister, entity);
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource)

Example 32 with EventSource

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

the class JpaPostUpdateEventListener method onPostUpdateCollection.

@Override
public void onPostUpdateCollection(PostCollectionUpdateEvent event) {
    Object entity = event.getCollection().getOwner();
    EventSource eventSource = event.getSession();
    handlePostUpdate(entity, eventSource);
}
Also used : EventSource(org.hibernate.event.spi.EventSource)

Example 33 with EventSource

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

the class JpaPostUpdateEventListener method onPostRecreateCollection.

@Override
public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
    Object entity = event.getCollection().getOwner();
    EventSource eventSource = event.getSession();
    handlePostUpdate(entity, eventSource);
}
Also used : EventSource(org.hibernate.event.spi.EventSource)

Example 34 with EventSource

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

the class JpaPostUpdateEventListener method onPostRemoveCollection.

@Override
public void onPostRemoveCollection(PostCollectionRemoveEvent event) {
    Object entity = event.getCollection().getOwner();
    EventSource eventSource = event.getSession();
    handlePostUpdate(entity, eventSource);
}
Also used : EventSource(org.hibernate.event.spi.EventSource)

Example 35 with EventSource

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

Aggregations

EventSource (org.hibernate.event.spi.EventSource)36 EntityPersister (org.hibernate.persister.entity.EntityPersister)15 Serializable (java.io.Serializable)10 EntityEntry (org.hibernate.engine.spi.EntityEntry)10 EntityKey (org.hibernate.engine.spi.EntityKey)8 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)7 PostLoadEvent (org.hibernate.event.spi.PostLoadEvent)5 HibernateProxy (org.hibernate.proxy.HibernateProxy)4 AssertionFailure (org.hibernate.AssertionFailure)3 HibernateException (org.hibernate.HibernateException)3 Status (org.hibernate.engine.spi.Status)3 PreLoadEvent (org.hibernate.event.spi.PreLoadEvent)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 Type (org.hibernate.type.Type)3 TransientObjectException (org.hibernate.TransientObjectException)2 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)2 CascadePoint (org.hibernate.engine.internal.CascadePoint)2 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 EventType (org.hibernate.event.spi.EventType)2