use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.
the class ReactiveEntityDeleteAction method reactiveExecute.
@Override
public CompletionStage<Void> reactiveExecute() throws HibernateException {
final Serializable id = getId();
final EntityPersister persister = getPersister();
final SharedSessionContractImplementor session = getSession();
final Object instance = getInstance();
final boolean veto = preDelete();
Object version = getVersion();
if (persister.isVersionPropertyGenerated()) {
// we need to grab the version value from the entity, otherwise
// we have issues with generated-version entities that may have
// multiple actions queued during the same flush
version = persister.getVersion(instance);
}
final Object ck;
if (persister.canWriteToCache()) {
final EntityDataAccess cache = persister.getCacheAccessStrategy();
ck = cache.generateCacheKey(id, persister, session.getFactory(), session.getTenantIdentifier());
setLock(cache.lockItem(session, ck, version));
} else {
ck = null;
}
CompletionStage<Void> deleteStep = !isCascadeDeleteEnabled() && !veto ? ((ReactiveEntityPersister) persister).deleteReactive(id, version, instance, session) : voidFuture();
return deleteStep.thenAccept(v -> {
// postDelete:
// After actually deleting a row, record the fact that the instance no longer
// exists on the database (needed for identity-column key generation), and
// remove it from the session cache
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
final EntityEntry entry = persistenceContext.removeEntry(instance);
if (entry == null) {
throw new AssertionFailure("possible non-threadsafe access to session");
}
entry.postDelete();
persistenceContext.removeEntity(entry.getEntityKey());
persistenceContext.removeProxy(entry.getEntityKey());
if (persister.canWriteToCache()) {
persister.getCacheAccessStrategy().remove(session, ck);
}
persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference(persister, id, getNaturalIdValues());
postDelete();
final StatisticsImplementor statistics = getSession().getFactory().getStatistics();
if (statistics.isStatisticsEnabled() && !veto) {
statistics.deleteEntity(getPersister().getEntityName());
}
});
}
use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.
the class DefaultReactiveLoadEventListener method reactiveOnLoad.
/**
* Handle the given load event.
*
* @param event The load event to be handled.
*/
@Override
public CompletionStage<Void> reactiveOnLoad(final LoadEvent event, final LoadEventListener.LoadType loadType) throws HibernateException {
final ReactiveEntityPersister persister = (ReactiveEntityPersister) getPersister(event);
if (persister == null) {
throw LOG.unableToLocatePersister(event.getEntityClassName());
}
CompletionStage<Void> result = checkId(event, loadType, persister).thenCompose(vd -> doOnLoad(persister, event, loadType).thenAccept(event::setResult).handle((v, x) -> {
if (event.getResult() instanceof CompletionStage) {
throw new AssertionFailure("Unexpected CompletionStage");
}
if (x instanceof HibernateException) {
LOG.unableToLoadCommand((HibernateException) x);
}
return returnNullorRethrow(x);
}));
// to go back to the database immediately and update the row
if (event.getLockMode() == LockMode.PESSIMISTIC_FORCE_INCREMENT || event.getLockMode() == LockMode.FORCE) {
// TODO: should we call CachedDomainDataAccess.lockItem() ?
return result.thenCompose(v -> persister.lockReactive(event.getEntityId(), persister.getVersion(event.getResult()), event.getResult(), event.getLockOptions(), event.getSession()));
} else {
return result;
}
}
use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.
the class ReactiveEntityIdentityInsertAction method reactiveExecute.
@Override
public CompletionStage<Void> reactiveExecute() throws HibernateException {
CompletionStage<Void> stage = reactiveNullifyTransientReferencesIfNotAlready();
final EntityPersister persister = getPersister();
final SharedSessionContractImplementor session = getSession();
final Object instance = getInstance();
setVeto(preInsert());
if (!isVeto()) {
ReactiveEntityPersister reactivePersister = (ReactiveEntityPersister) persister;
return stage.thenCompose(v -> reactivePersister.insertReactive(getState(), instance, session)).thenApply(this::applyGeneratedId).thenCompose(generatedId -> processInsertGenerated(reactivePersister, generatedId, instance, session).thenApply(v -> generatedId)).thenAccept(generatedId -> {
// need to do that here rather than in the save event listener to let
// the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
persister.setIdentifier(instance, generatedId, session);
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
persistenceContext.registerInsertedKey(getPersister(), generatedId);
EntityKey entityKey = session.generateEntityKey(generatedId, persister);
setEntityKey(entityKey);
persistenceContext.checkUniqueness(entityKey, getInstance());
postInsert();
final StatisticsImplementor statistics = session.getFactory().getStatistics();
if (statistics.isStatisticsEnabled() && !isVeto()) {
statistics.insertEntity(getPersister().getEntityName());
}
markExecuted();
});
} else {
postInsert();
markExecuted();
return stage;
}
}
use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.
the class ReactiveEntityRegularInsertAction method reactiveExecute.
@Override
public CompletionStage<Void> reactiveExecute() throws HibernateException {
return reactiveNullifyTransientReferencesIfNotAlready().thenCompose(v -> {
EntityPersister persister = getPersister();
final SharedSessionContractImplementor session = getSession();
final Object instance = getInstance();
final Serializable id = getId();
// FIXME: It needs to become async
final boolean veto = preInsert();
// Don't need to lock the cache here, since if someone
// else inserted the same pk first, the insert would fail
CompletionStage<Void> insertStage;
if (!veto) {
ReactiveEntityPersister reactivePersister = (ReactiveEntityPersister) persister;
insertStage = reactivePersister.insertReactive(id, getState(), instance, session).thenApply(res -> {
EntityEntry entry = session.getPersistenceContext().getEntry(instance);
if (entry == null) {
throw new AssertionFailure("possible non-threadsafe access to session");
}
entry.postInsert(getState());
return entry;
}).thenCompose(entry -> processInsertGeneratedProperties(reactivePersister, session, instance, id, entry).thenAccept(vv -> session.getPersistenceContext().registerInsertedKey(persister, getId())));
} else {
insertStage = voidFuture();
}
return insertStage.thenApply(res -> {
final SessionFactoryImplementor factory = session.getFactory();
if (isCachePutEnabled(persister, session)) {
final CacheEntry ce = persister.buildCacheEntry(instance, getState(), getVersion(), session);
setCacheEntry(persister.getCacheEntryStructure().structure(ce));
final EntityDataAccess cache = persister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(id, persister, factory, session.getTenantIdentifier());
final boolean put = cacheInsert(persister, ck);
if (put && factory.getStatistics().isStatisticsEnabled()) {
factory.getStatistics().entityCachePut(persister.getNavigableRole(), persister.getCacheAccessStrategy().getRegion().getName());
}
}
handleNaturalIdPostSaveNotifications(id);
postInsert();
if (factory.getStatistics().isStatisticsEnabled() && !veto) {
factory.getStatistics().insertEntity(getEntityName());
}
markExecuted();
return null;
});
});
}
use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.
the class DefaultReactiveResolveNaturalIdEventListener method loadFromDatasource.
/**
* Performs the process of loading an entity from the configured
* underlying datasource.
*
* @param event The load event
*
* @return The object loaded from the datasource, or null if not found.
*/
protected CompletionStage<Serializable> loadFromDatasource(ResolveNaturalIdEvent event) {
EventSource session = event.getSession();
StatisticsImplementor statistics = session.getFactory().getStatistics();
boolean statisticsEnabled = statistics.isStatisticsEnabled();
long startTime = statisticsEnabled ? System.nanoTime() : 0;
EntityPersister entityPersister = event.getEntityPersister();
Object[] orderedNaturalIdValues = event.getOrderedNaturalIdValues();
return ((ReactiveEntityPersister) entityPersister).reactiveLoadEntityIdByNaturalId(orderedNaturalIdValues, event.getLockOptions(), session).thenApply(pk -> {
if (statisticsEnabled) {
statistics.naturalIdQueryExecuted(entityPersister.getRootEntityName(), MILLISECONDS.convert(System.nanoTime() - startTime, NANOSECONDS));
}
// PK can be null if the entity doesn't exist
if (pk != null) {
getNaturalIdHelper(event).cacheNaturalIdCrossReferenceFromLoad(entityPersister, pk, orderedNaturalIdValues);
}
return pk;
});
}
Aggregations