Search in sources :

Example 26 with EventSource

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

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

the class AbstractFlushingEventListener method logFlushResults.

@SuppressWarnings(value = { "unchecked" })
private void logFlushResults(FlushEvent event) {
    if (!LOG.isDebugEnabled()) {
        return;
    }
    final EventSource session = event.getSession();
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    LOG.debugf("Flushed: %s insertions, %s updates, %s deletions to %s objects", session.getActionQueue().numberOfInsertions(), session.getActionQueue().numberOfUpdates(), session.getActionQueue().numberOfDeletions(), persistenceContext.getNumberOfManagedEntities());
    LOG.debugf("Flushed: %s (re)creations, %s updates, %s removals to %s collections", session.getActionQueue().numberOfCollectionCreations(), session.getActionQueue().numberOfCollectionUpdates(), session.getActionQueue().numberOfCollectionRemovals(), persistenceContext.getCollectionEntries().size());
    new EntityPrinter(session.getFactory()).toString(persistenceContext.getEntitiesByKey().entrySet());
}
Also used : EventSource(org.hibernate.event.spi.EventSource) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) EntityPrinter(org.hibernate.internal.util.EntityPrinter)

Example 28 with EventSource

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

the class DefaultLockEventListener method cascadeOnLock.

private void cascadeOnLock(LockEvent event, EntityPersister persister, Object entity) {
    EventSource source = event.getSession();
    source.getPersistenceContext().incrementCascadeLevel();
    try {
        Cascade.cascade(CascadingActions.LOCK, CascadePoint.AFTER_LOCK, source, persister, entity, event.getLockOptions());
    } finally {
        source.getPersistenceContext().decrementCascadeLevel();
    }
}
Also used : EventSource(org.hibernate.event.spi.EventSource)

Example 29 with EventSource

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

the class DefaultMergeEventListener method entityIsPersistent.

protected void entityIsPersistent(MergeEvent event, Map copyCache) {
    LOG.trace("Ignoring persistent instance");
    //TODO: check that entry.getIdentifier().equals(requestedId)
    final Object entity = event.getEntity();
    final EventSource source = event.getSession();
    final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
    //beforeQuery cascade!
    ((MergeContext) copyCache).put(entity, entity, true);
    cascadeOnMerge(source, persister, entity, copyCache);
    copyValues(persister, entity, entity, source, copyCache);
    event.setResult(entity);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EventSource(org.hibernate.event.spi.EventSource)

Example 30 with EventSource

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

the class DefaultSaveOrUpdateEventListener method cascadeOnUpdate.

/**
	 * Handles the calls needed to perform cascades as part of an update request
	 * for the given entity.
	 *
	 * @param event The event currently being processed.
	 * @param persister The defined persister for the entity being updated.
	 * @param entity The entity being updated.
	 */
private void cascadeOnUpdate(SaveOrUpdateEvent event, EntityPersister persister, Object entity) {
    final EventSource source = event.getSession();
    source.getPersistenceContext().incrementCascadeLevel();
    try {
        Cascade.cascade(CascadingActions.SAVE_UPDATE, CascadePoint.AFTER_UPDATE, source, persister, entity);
    } finally {
        source.getPersistenceContext().decrementCascadeLevel();
    }
}
Also used : EventSource(org.hibernate.event.spi.EventSource)

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