Search in sources :

Example 1 with ReactiveActionQueue

use of org.hibernate.reactive.engine.ReactiveActionQueue in project hibernate-reactive by hibernate.

the class DefaultReactiveDeleteEventListener method deleteEntity.

/**
 * Perform the entity deletion.  Well, as with most operations, does not
 * really perform it; just schedules an action/execution with the
 * {@link org.hibernate.engine.spi.ActionQueue} for execution during flush.
 *
 * @param session The originating session
 * @param entity The entity to delete
 * @param entityEntry The entity's entry in the {@link PersistenceContext}
 * @param isCascadeDeleteEnabled Is delete cascading enabled?
 * @param persister The entity persister.
 * @param transientEntities A cache of already deleted entities.
 */
protected CompletionStage<Void> deleteEntity(final EventSource session, final Object entity, final EntityEntry entityEntry, final boolean isCascadeDeleteEnabled, final boolean isOrphanRemovalBeforeUpdates, final EntityPersister persister, final IdentitySet transientEntities) {
    if (LOG.isTraceEnabled()) {
        LOG.tracev("Deleting {0}", infoString(persister, entityEntry.getId(), session.getFactory()));
    }
    final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
    final Type[] propTypes = persister.getPropertyTypes();
    final Object version = entityEntry.getVersion();
    final Object[] currentState;
    if (entityEntry.getLoadedState() == null) {
        // ie. the entity came in from update()
        currentState = persister.getPropertyValues(entity);
    } else {
        currentState = entityEntry.getLoadedState();
    }
    final Object[] deletedState = createDeletedState(persister, currentState, session);
    entityEntry.setDeletedState(deletedState);
    session.getInterceptor().onDelete(entity, entityEntry.getId(), deletedState, persister.getPropertyNames(), propTypes);
    // before any callbacks, etc, so subdeletions see that this deletion happened first
    persistenceContext.setEntryStatus(entityEntry, Status.DELETED);
    final EntityKey key = session.generateEntityKey(entityEntry.getId(), persister);
    CompletionStage<Void> beforeDelete = cascadeBeforeDelete(session, persister, entity, entityEntry, transientEntities);
    CompletionStage<Void> nullifyAndAction = new ForeignKeys.Nullifier(entity, true, false, session, persister).nullifyTransientReferences(entityEntry.getDeletedState()).thenAccept(v -> {
        new Nullability(session).checkNullability(entityEntry.getDeletedState(), persister, Nullability.NullabilityCheckType.DELETE);
        persistenceContext.registerNullifiableEntityKey(key);
        ReactiveActionQueue actionQueue = actionQueue(session);
        if (isOrphanRemovalBeforeUpdates) {
            // TODO: The removeOrphan concept is a temporary "hack" for HHH-6484.  This should be removed once action/task
            // ordering is improved.
            actionQueue.addAction(new ReactiveOrphanRemovalAction(entityEntry.getId(), deletedState, version, entity, persister, isCascadeDeleteEnabled, session));
        } else {
            // Ensures that containing deletions happen before sub-deletions
            actionQueue.addAction(new ReactiveEntityDeleteAction(entityEntry.getId(), deletedState, version, entity, persister, isCascadeDeleteEnabled, session));
        }
    });
    CompletionStage<Void> afterDelete = cascadeAfterDelete(session, persister, entity, transientEntities);
    return beforeDelete.thenCompose(v -> nullifyAndAction).thenCompose(v -> afterDelete);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ReactiveActionQueue(org.hibernate.reactive.engine.ReactiveActionQueue) ReactiveEntityDeleteAction(org.hibernate.reactive.engine.impl.ReactiveEntityDeleteAction) Nullability(org.hibernate.engine.internal.Nullability) EventSource(org.hibernate.event.spi.EventSource) Log(org.hibernate.reactive.logging.impl.Log) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) JpaBootstrapSensitive(org.hibernate.event.service.spi.JpaBootstrapSensitive) LoggerFactory(org.hibernate.reactive.logging.impl.LoggerFactory) DeleteEventListener(org.hibernate.event.spi.DeleteEventListener) OnUpdateVisitor(org.hibernate.event.internal.OnUpdateVisitor) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) Cascade(org.hibernate.reactive.engine.impl.Cascade) Cascade.fetchLazyAssociationsBeforeCascade(org.hibernate.reactive.engine.impl.Cascade.fetchLazyAssociationsBeforeCascade) Status(org.hibernate.engine.spi.Status) DeleteEvent(org.hibernate.event.spi.DeleteEvent) CompletionStages.voidFuture(org.hibernate.reactive.util.impl.CompletionStages.voidFuture) ReactiveSession(org.hibernate.reactive.session.ReactiveSession) LockMode(org.hibernate.LockMode) MethodHandles(java.lang.invoke.MethodHandles) CallbackRegistry(org.hibernate.jpa.event.spi.CallbackRegistry) Set(java.util.Set) ForeignKeys(org.hibernate.reactive.engine.impl.ForeignKeys) ReactiveOrphanRemovalAction(org.hibernate.reactive.engine.impl.ReactiveOrphanRemovalAction) TypeHelper(org.hibernate.type.TypeHelper) EntityKey(org.hibernate.engine.spi.EntityKey) Serializable(java.io.Serializable) TransientObjectException(org.hibernate.TransientObjectException) MessageHelper.infoString(org.hibernate.pretty.MessageHelper.infoString) CascadingActions(org.hibernate.reactive.engine.impl.CascadingActions) CompletionStage(java.util.concurrent.CompletionStage) CallbackRegistryConsumer(org.hibernate.jpa.event.spi.CallbackRegistryConsumer) EntityEntry(org.hibernate.engine.spi.EntityEntry) CascadePoint(org.hibernate.engine.internal.CascadePoint) HibernateException(org.hibernate.HibernateException) ReactiveDeleteEventListener(org.hibernate.reactive.event.ReactiveDeleteEventListener) Type(org.hibernate.type.Type) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) Nullability(org.hibernate.engine.internal.Nullability) ReactiveOrphanRemovalAction(org.hibernate.reactive.engine.impl.ReactiveOrphanRemovalAction) EntityKey(org.hibernate.engine.spi.EntityKey) Type(org.hibernate.type.Type) ReactiveActionQueue(org.hibernate.reactive.engine.ReactiveActionQueue) ReactiveEntityDeleteAction(org.hibernate.reactive.engine.impl.ReactiveEntityDeleteAction)

Example 2 with ReactiveActionQueue

use of org.hibernate.reactive.engine.ReactiveActionQueue in project hibernate-reactive by hibernate.

the class AbstractReactiveFlushingEventListener method flushCollections.

/**
 * process any unreferenced collections and then inspect all known collections,
 * scheduling creates/removes/updates
 */
private int flushCollections(final EventSource session, final PersistenceContext persistenceContext) throws HibernateException {
    LOG.trace("Processing unreferenced collections");
    final int count = persistenceContext.getCollectionEntriesSize();
    persistenceContext.forEachCollectionEntry((persistentCollection, collectionEntry) -> {
        if (!collectionEntry.isReached() && !collectionEntry.isIgnore()) {
            Collections.processUnreachableCollection(persistentCollection, session);
        }
    }, true);
    // Schedule updates to collections:
    LOG.trace("Scheduling collection removes/(re)creates/updates");
    final ReactiveActionQueue actionQueue = session.unwrap(ReactiveSession.class).getReactiveActionQueue();
    final Interceptor interceptor = session.getInterceptor();
    persistenceContext.forEachCollectionEntry((coll, ce) -> {
        if (ce.isDorecreate()) {
            interceptor.onCollectionRecreate(coll, ce.getCurrentKey());
            actionQueue.addAction(new ReactiveCollectionRecreateAction(coll, ce.getCurrentPersister(), ce.getCurrentKey(), session));
        }
        if (ce.isDoremove()) {
            interceptor.onCollectionRemove(coll, ce.getLoadedKey());
            actionQueue.addAction(new ReactiveCollectionRemoveAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), ce.isSnapshotEmpty(coll), session));
        }
        if (ce.isDoupdate()) {
            interceptor.onCollectionUpdate(coll, ce.getLoadedKey());
            actionQueue.addAction(new ReactiveCollectionUpdateAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), ce.isSnapshotEmpty(coll), session));
        }
        // todo : I'm not sure the !wasInitialized part should really be part of this check
        if (!coll.wasInitialized() && coll.hasQueuedOperations()) {
            actionQueue.addAction(new QueuedOperationCollectionAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), session));
        }
    }, true);
    actionQueue.sortCollectionActions();
    return count;
}
Also used : ReactiveSession(org.hibernate.reactive.session.ReactiveSession) ReactiveActionQueue(org.hibernate.reactive.engine.ReactiveActionQueue) ReactiveCollectionRemoveAction(org.hibernate.reactive.engine.impl.ReactiveCollectionRemoveAction) ReactiveCollectionUpdateAction(org.hibernate.reactive.engine.impl.ReactiveCollectionUpdateAction) QueuedOperationCollectionAction(org.hibernate.action.internal.QueuedOperationCollectionAction) ReactiveCollectionRecreateAction(org.hibernate.reactive.engine.impl.ReactiveCollectionRecreateAction) Interceptor(org.hibernate.Interceptor) CascadePoint(org.hibernate.engine.internal.CascadePoint)

Aggregations

CascadePoint (org.hibernate.engine.internal.CascadePoint)2 ReactiveActionQueue (org.hibernate.reactive.engine.ReactiveActionQueue)2 ReactiveSession (org.hibernate.reactive.session.ReactiveSession)2 Serializable (java.io.Serializable)1 MethodHandles (java.lang.invoke.MethodHandles)1 Set (java.util.Set)1 CompletionStage (java.util.concurrent.CompletionStage)1 HibernateException (org.hibernate.HibernateException)1 Interceptor (org.hibernate.Interceptor)1 LockMode (org.hibernate.LockMode)1 TransientObjectException (org.hibernate.TransientObjectException)1 QueuedOperationCollectionAction (org.hibernate.action.internal.QueuedOperationCollectionAction)1 Nullability (org.hibernate.engine.internal.Nullability)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 Status (org.hibernate.engine.spi.Status)1 OnUpdateVisitor (org.hibernate.event.internal.OnUpdateVisitor)1 JpaBootstrapSensitive (org.hibernate.event.service.spi.JpaBootstrapSensitive)1 DeleteEvent (org.hibernate.event.spi.DeleteEvent)1