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();
});
}
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());
}
});
}
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());
}
});
}
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);
}
}
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();
}
Aggregations