Search in sources :

Example 1 with ReactiveCollectionPersister

use of org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister in project hibernate-reactive by hibernate.

the class ReactiveCollectionRecreateAction method reactiveExecute.

@Override
public CompletionStage<Void> reactiveExecute() {
    final ReactiveCollectionPersister persister = (ReactiveCollectionPersister) getPersister();
    final SharedSessionContractImplementor session = getSession();
    final Serializable key = getKey();
    final PersistentCollection collection = getCollection();
    preRecreate();
    return persister.recreateReactive(collection, key, session).thenAccept(v -> {
        session.getPersistenceContextInternal().getCollectionEntry(collection).afterAction(collection);
        evict();
        postRecreate();
    });
}
Also used : ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Example 2 with ReactiveCollectionPersister

use of org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister in project hibernate-reactive by hibernate.

the class ReactiveCollectionRemoveAction method reactiveExecute.

@Override
public CompletionStage<Void> reactiveExecute() {
    final Serializable key = getKey();
    final SharedSessionContractImplementor session = getSession();
    final ReactiveCollectionPersister reactivePersister = (ReactiveCollectionPersister) getPersister();
    final CollectionPersister corePersister = getPersister();
    final PersistentCollection collection = getCollection();
    final StatisticsImplementor statistics = session.getFactory().getStatistics();
    CompletionStage<Void> removeStage = CompletionStages.voidFuture();
    if (!emptySnapshot) {
        // an existing collection that was either non-empty or uninitialized
        // is replaced by null or a different collection
        // (if the collection is uninitialized, hibernate has no way of
        // knowing if the collection is actually empty without querying the db)
        removeStage = removeStage.thenAccept(v -> preRemove()).thenCompose(v -> reactivePersister.removeReactive(key, session).thenAccept(ignore -> {
            evict();
            postRemove();
        }));
    }
    if (collection != null) {
        return removeStage.thenAccept(v -> {
            session.getPersistenceContextInternal().getCollectionEntry(collection).afterAction(collection);
            evict();
            postRemove();
            if (statistics.isStatisticsEnabled()) {
                statistics.updateCollection(corePersister.getRole());
            }
        });
    }
    return removeStage.thenAccept(v -> {
        evict();
        postRemove();
        if (statistics.isStatisticsEnabled()) {
            statistics.updateCollection(corePersister.getRole());
        }
    });
}
Also used : ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) EventListenerGroup(org.hibernate.event.service.spi.EventListenerGroup) CollectionAction(org.hibernate.action.internal.CollectionAction) AssertionFailure(org.hibernate.AssertionFailure) ReactiveExecutable(org.hibernate.reactive.engine.ReactiveExecutable) PreCollectionRecreateEventListener(org.hibernate.event.spi.PreCollectionRecreateEventListener) PreCollectionRemoveEventListener(org.hibernate.event.spi.PreCollectionRemoveEventListener) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) PostCollectionRemoveEvent(org.hibernate.event.spi.PostCollectionRemoveEvent) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) Serializable(java.io.Serializable) PreCollectionRecreateEvent(org.hibernate.event.spi.PreCollectionRecreateEvent) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) PostCollectionRemoveEventListener(org.hibernate.event.spi.PostCollectionRemoveEventListener) CompletionStage(java.util.concurrent.CompletionStage) PreCollectionRemoveEvent(org.hibernate.event.spi.PreCollectionRemoveEvent) PostCollectionRecreateEventListener(org.hibernate.event.spi.PostCollectionRecreateEventListener) EventType(org.hibernate.event.spi.EventType) PostCollectionRecreateEvent(org.hibernate.event.spi.PostCollectionRecreateEvent) HibernateException(org.hibernate.HibernateException) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CompletionStages(org.hibernate.reactive.util.impl.CompletionStages) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor)

Example 3 with ReactiveCollectionPersister

use of org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister in project hibernate-reactive by hibernate.

the class ReactiveCollectionUpdateAction method reactiveExecute.

@Override
public CompletionStage<Void> reactiveExecute() {
    final Serializable key = getKey();
    final SharedSessionContractImplementor session = getSession();
    final ReactiveCollectionPersister reactivePersister = (ReactiveCollectionPersister) getPersister();
    final CollectionPersister corePersister = getPersister();
    final PersistentCollection collection = getCollection();
    final boolean affectedByFilters = corePersister.isAffectedByEnabledFilters(session);
    preUpdate();
    CompletionStage<Void> updateStage = CompletionStages.voidFuture();
    // And then make sure that each operations is executed in its own stage maintaining the same order as in ORM
    if (!collection.wasInitialized()) {
        // The collection should still be dirty.
        if (!collection.isDirty()) {
            throw new AssertionFailure("collection is not dirty");
        }
    // do nothing - we only need to notify the cache...
    } else if (!affectedByFilters && collection.empty()) {
        if (!emptySnapshot) {
            updateStage = updateStage.thenCompose(v -> reactivePersister.removeReactive(key, session)).thenAccept(count -> {
            /* We don't care, maybe we can log it as debug */
            });
        }
    } else if (collection.needsRecreate(corePersister)) {
        if (affectedByFilters) {
            throw LOG.cannotRecreateCollectionWhileFilterIsEnabled(collectionInfoString(corePersister, collection, key, session));
        }
        if (!emptySnapshot) {
            updateStage = updateStage.thenCompose(v -> reactivePersister.removeReactive(key, session)).thenAccept(count -> {
            /* We don't care, maybe we can log it as debug */
            });
        }
        return updateStage.thenCompose(v -> reactivePersister.recreateReactive(collection, key, session).thenAccept(ignore -> {
            session.getPersistenceContextInternal().getCollectionEntry(collection).afterAction(collection);
            evict();
            postUpdate();
            final StatisticsImplementor statistics = session.getFactory().getStatistics();
            if (statistics.isStatisticsEnabled()) {
                statistics.updateCollection(corePersister.getRole());
            }
        }));
    } else {
        updateStage = updateStage.thenCompose(v -> reactivePersister.reactiveDeleteRows(collection, key, session)).thenCompose(v -> reactivePersister.reactiveUpdateRows(collection, key, session)).thenCompose(v -> reactivePersister.reactiveInsertRows(collection, key, session));
    }
    return updateStage.thenAccept(v -> {
        session.getPersistenceContextInternal().getCollectionEntry(collection).afterAction(collection);
        evict();
        postUpdate();
        final StatisticsImplementor statistics = session.getFactory().getStatistics();
        if (statistics.isStatisticsEnabled()) {
            statistics.updateCollection(corePersister.getRole());
        }
    });
}
Also used : ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) EventListenerGroup(org.hibernate.event.service.spi.EventListenerGroup) CollectionAction(org.hibernate.action.internal.CollectionAction) AssertionFailure(org.hibernate.AssertionFailure) ReactiveExecutable(org.hibernate.reactive.engine.ReactiveExecutable) PreCollectionUpdateEventListener(org.hibernate.event.spi.PreCollectionUpdateEventListener) PreCollectionRecreateEventListener(org.hibernate.event.spi.PreCollectionRecreateEventListener) Log(org.hibernate.reactive.logging.impl.Log) LoggerFactory(org.hibernate.reactive.logging.impl.LoggerFactory) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EventType(org.hibernate.event.spi.EventType) PreCollectionUpdateEvent(org.hibernate.event.spi.PreCollectionUpdateEvent) MessageHelper.collectionInfoString(org.hibernate.pretty.MessageHelper.collectionInfoString) MethodHandles(java.lang.invoke.MethodHandles) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) Serializable(java.io.Serializable) PreCollectionRecreateEvent(org.hibernate.event.spi.PreCollectionRecreateEvent) PostCollectionUpdateEvent(org.hibernate.event.spi.PostCollectionUpdateEvent) CompletionStage(java.util.concurrent.CompletionStage) PostCollectionRecreateEventListener(org.hibernate.event.spi.PostCollectionRecreateEventListener) PostCollectionUpdateEventListener(org.hibernate.event.spi.PostCollectionUpdateEventListener) PostCollectionRecreateEvent(org.hibernate.event.spi.PostCollectionRecreateEvent) HibernateException(org.hibernate.HibernateException) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CompletionStages(org.hibernate.reactive.util.impl.CompletionStages) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AssertionFailure(org.hibernate.AssertionFailure) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor)

Example 4 with ReactiveCollectionPersister

use of org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister in project hibernate-reactive by hibernate.

the class ReactiveStatelessSessionImpl method reactiveFetch.

@Override
@SuppressWarnings("unchecked")
public <T> CompletionStage<T> reactiveFetch(T association, boolean unproxy) {
    checkOpen();
    PersistenceContext persistenceContext = getPersistenceContext();
    if (association instanceof HibernateProxy) {
        LazyInitializer initializer = ((HibernateProxy) association).getHibernateLazyInitializer();
        if (!initializer.isUninitialized()) {
            return completedFuture(unproxy ? (T) initializer.getImplementation() : association);
        } else {
            String entityName = initializer.getEntityName();
            Serializable id = initializer.getIdentifier();
            ReactiveEntityPersister persister = (ReactiveEntityPersister) getFactory().getMetamodel().entityPersister(entityName);
            initializer.setSession(this);
            persistenceContext.beforeLoad();
            return persister.reactiveLoad(id, initializer.getImplementation(), LockOptions.NONE, this).whenComplete((v, e) -> {
                persistenceContext.afterLoad();
                if (persistenceContext.isLoadFinished()) {
                    persistenceContext.clear();
                }
            }).thenApply(entity -> {
                checkEntityFound(this, entityName, id, entity);
                initializer.setImplementation(entity);
                initializer.unsetSession();
                return unproxy ? (T) entity : association;
            });
        }
    } else if (association instanceof PersistentCollection) {
        PersistentCollection persistentCollection = (PersistentCollection) association;
        if (persistentCollection.wasInitialized()) {
            return completedFuture(association);
        } else {
            ReactiveCollectionPersister persister = (ReactiveCollectionPersister) getFactory().getMetamodel().collectionPersister(persistentCollection.getRole());
            Serializable key = persistentCollection.getKey();
            persistenceContext.addUninitializedCollection(persister, persistentCollection, key);
            persistentCollection.setCurrentSession(this);
            return persister.reactiveInitialize(key, this).whenComplete((v, e) -> {
                if (persistenceContext.isLoadFinished()) {
                    persistenceContext.clear();
                }
            }).thenApply(v -> association);
        }
    } else {
        return completedFuture(association);
    }
}
Also used : CompletionStages.completedFuture(org.hibernate.reactive.util.impl.CompletionStages.completedFuture) BatchingConnection(org.hibernate.reactive.pool.BatchingConnection) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) EntityPersister(org.hibernate.persister.entity.EntityPersister) NativeQueryTupleTransformer(org.hibernate.jpa.spi.NativeQueryTupleTransformer) GraphSemantic(org.hibernate.graph.GraphSemantic) IdentifierGeneration.assignIdIfNecessary(org.hibernate.reactive.id.impl.IdentifierGeneration.assignIdIfNecessary) Log(org.hibernate.reactive.logging.impl.Log) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) LoggerFactory(org.hibernate.reactive.logging.impl.LoggerFactory) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) SQLCustomQuery(org.hibernate.loader.custom.sql.SQLCustomQuery) ReactiveCustomLoader(org.hibernate.reactive.loader.custom.impl.ReactiveCustomLoader) HibernateProxy(org.hibernate.proxy.HibernateProxy) CustomQuery(org.hibernate.loader.custom.CustomQuery) HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) ReactiveNativeQuery(org.hibernate.reactive.session.ReactiveNativeQuery) BytecodeEnhancementMetadata(org.hibernate.bytecode.spi.BytecodeEnhancementMetadata) EntityGraph(javax.persistence.EntityGraph) ParameterMetadata(org.hibernate.query.ParameterMetadata) LockOptions(org.hibernate.LockOptions) MethodHandles(java.lang.invoke.MethodHandles) ResultSetMapping(org.hibernate.reactive.common.ResultSetMapping) ReactiveStatelessSession(org.hibernate.reactive.session.ReactiveStatelessSession) Serializable(java.io.Serializable) List(java.util.List) CompletionStages.failedFuture(org.hibernate.reactive.util.impl.CompletionStages.failedFuture) CompletionStage(java.util.concurrent.CompletionStage) Dialect(org.hibernate.dialect.Dialect) CompletionStages.loop(org.hibernate.reactive.util.impl.CompletionStages.loop) ReactiveEntityPersister(org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister) HibernateException(org.hibernate.HibernateException) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Criteria(org.hibernate.reactive.session.Criteria) NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) SessionUtil.checkEntityFound(org.hibernate.reactive.session.impl.SessionUtil.checkEntityFound) RootGraphImpl(org.hibernate.graph.internal.RootGraphImpl) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) CompletableFuture(java.util.concurrent.CompletableFuture) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) CriteriaQueryTupleTransformer(org.hibernate.jpa.spi.CriteriaQueryTupleTransformer) ReactiveQuery(org.hibernate.reactive.session.ReactiveQuery) IdentifierGeneration.generateId(org.hibernate.reactive.id.impl.IdentifierGeneration.generateId) BulkOperationCleanupAction(org.hibernate.action.internal.BulkOperationCleanupAction) StatelessSessionImpl(org.hibernate.internal.StatelessSessionImpl) MetamodelImplementor(org.hibernate.metamodel.spi.MetamodelImplementor) Tuple(javax.persistence.Tuple) QueryParameters(org.hibernate.engine.spi.QueryParameters) LockMode(org.hibernate.LockMode) LazyInitializer(org.hibernate.proxy.LazyInitializer) Versioning(org.hibernate.engine.internal.Versioning) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) EntityKey(org.hibernate.engine.spi.EntityKey) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) CriteriaQueryOptions(org.hibernate.reactive.session.CriteriaQueryOptions) ReactivePersistenceContextAdapter(org.hibernate.reactive.engine.impl.ReactivePersistenceContextAdapter) SessionCreationOptions(org.hibernate.internal.SessionCreationOptions) NativeSQLQuerySpecification(org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification) ReactiveConnection(org.hibernate.reactive.pool.ReactiveConnection) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) LazyInitializer(org.hibernate.proxy.LazyInitializer) Serializable(java.io.Serializable) ReactiveEntityPersister(org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 5 with ReactiveCollectionPersister

use of org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister in project hibernate-reactive by hibernate.

the class DefaultReactiveInitializeCollectionEventListener method onReactiveInitializeCollection.

/**
 * called by a collection that wants to initialize itself
 */
public CompletionStage<Void> onReactiveInitializeCollection(InitializeCollectionEvent event) throws HibernateException {
    PersistentCollection collection = event.getCollection();
    SessionImplementor source = event.getSession();
    CollectionEntry ce = source.getPersistenceContextInternal().getCollectionEntry(collection);
    if (ce == null) {
        throw LOG.collectionWasEvicted();
    }
    if (!collection.wasInitialized()) {
        final CollectionPersister loadedPersister = ce.getLoadedPersister();
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Initializing collection {0}", collectionInfoString(loadedPersister, collection, ce.getLoadedKey(), source));
            LOG.trace("Checking second-level cache");
        }
        final boolean foundInCache = initializeCollectionFromCache(ce.getLoadedKey(), loadedPersister, collection, source);
        if (foundInCache) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Collection initialized from cache");
            }
            return voidFuture();
        } else {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Collection not cached");
            }
            return ((ReactiveCollectionPersister) loadedPersister).reactiveInitialize(ce.getLoadedKey(), source).thenAccept(list -> {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Collection initialized");
                }
                final StatisticsImplementor statistics = source.getFactory().getStatistics();
                if (statistics.isStatisticsEnabled()) {
                    statistics.fetchCollection(loadedPersister.getRole());
                }
            });
        }
    }
    // Collection was already initialized.
    return voidFuture();
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor)

Aggregations

PersistentCollection (org.hibernate.collection.spi.PersistentCollection)5 ReactiveCollectionPersister (org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister)5 Serializable (java.io.Serializable)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 CompletionStage (java.util.concurrent.CompletionStage)3 HibernateException (org.hibernate.HibernateException)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 MethodHandles (java.lang.invoke.MethodHandles)2 AssertionFailure (org.hibernate.AssertionFailure)2 CollectionAction (org.hibernate.action.internal.CollectionAction)2 EventListenerGroup (org.hibernate.event.service.spi.EventListenerGroup)2 EventType (org.hibernate.event.spi.EventType)2 PostCollectionRecreateEvent (org.hibernate.event.spi.PostCollectionRecreateEvent)2 PostCollectionRecreateEventListener (org.hibernate.event.spi.PostCollectionRecreateEventListener)2 PreCollectionRecreateEvent (org.hibernate.event.spi.PreCollectionRecreateEvent)2 PreCollectionRecreateEventListener (org.hibernate.event.spi.PreCollectionRecreateEventListener)2 ReactiveExecutable (org.hibernate.reactive.engine.ReactiveExecutable)2 CompletionStages (org.hibernate.reactive.util.impl.CompletionStages)2 StatisticsImplementor (org.hibernate.stat.spi.StatisticsImplementor)2 List (java.util.List)1