Search in sources :

Example 6 with ReactiveEntityPersister

use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.

the class ReactiveStatelessSessionImpl method reactiveRefresh.

@Override
public CompletionStage<Void> reactiveRefresh(Object entity, LockMode lockMode) {
    final ReactiveEntityPersister persister = getEntityPersister(null, entity);
    final Serializable id = persister.getIdentifier(entity, this);
    if (persister.canWriteToCache()) {
        final EntityDataAccess cacheAccess = persister.getCacheAccessStrategy();
        if (cacheAccess != null) {
            final Object ck = cacheAccess.generateCacheKey(id, persister, getFactory(), getTenantIdentifier());
            cacheAccess.evict(ck);
        }
    }
    String previousFetchProfile = getLoadQueryInfluencers().getInternalFetchProfile();
    getLoadQueryInfluencers().setInternalFetchProfile("refresh");
    return persister.reactiveLoad(id, entity, getNullSafeLockOptions(lockMode), this).thenAccept(result -> {
        if (getPersistenceContext().isLoadFinished()) {
            getPersistenceContext().clear();
        }
        UnresolvableObjectException.throwIfNull(result, id, persister.getEntityName());
    }).whenComplete((v, e) -> getLoadQueryInfluencers().setInternalFetchProfile(previousFetchProfile));
}
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) Serializable(java.io.Serializable) ReactiveEntityPersister(org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Example 7 with ReactiveEntityPersister

use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.

the class ReactiveStatelessSessionImpl method reactiveDelete.

@Override
public CompletionStage<Void> reactiveDelete(Object entity) {
    checkOpen();
    ReactiveEntityPersister persister = getEntityPersister(null, entity);
    Serializable id = persister.getIdentifier(entity, this);
    Object version = persister.getVersion(entity);
    return persister.deleteReactive(id, version, entity, this);
}
Also used : Serializable(java.io.Serializable) ReactiveEntityPersister(org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister)

Example 8 with ReactiveEntityPersister

use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.

the class ReactiveStatelessSessionImpl method executeReactiveUpdate.

/**
 * @param entity a detached entity or initialized proxy
 *
 * @return a void stage
 */
private CompletionStage<Void> executeReactiveUpdate(Object entity) {
    ReactiveEntityPersister persister = getEntityPersister(null, entity);
    Serializable id = persister.getIdentifier(entity, this);
    Object[] state = persister.getPropertyValues(entity);
    Object oldVersion;
    if (persister.isVersioned()) {
        oldVersion = persister.getVersion(entity);
        Object newVersion = Versioning.increment(oldVersion, persister.getVersionType(), this);
        Versioning.setVersion(state, newVersion, persister);
        persister.setPropertyValues(entity, state);
    } else {
        oldVersion = null;
    }
    return persister.updateReactive(id, state, null, false, null, oldVersion, entity, null, this);
}
Also used : Serializable(java.io.Serializable) ReactiveEntityPersister(org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister)

Example 9 with ReactiveEntityPersister

use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister 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 10 with ReactiveEntityPersister

use of org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister in project hibernate-reactive by hibernate.

the class ReactiveStatelessSessionImpl method reactiveInsert.

@Override
public CompletionStage<Void> reactiveInsert(Object entity) {
    checkOpen();
    ReactiveEntityPersister persister = getEntityPersister(null, entity);
    return generateId(entity, persister, this, this).thenCompose(id -> {
        Object[] state = persister.getPropertyValues(entity);
        if (persister.isVersioned()) {
            boolean substitute = Versioning.seedVersion(state, persister.getVersionProperty(), persister.getVersionType(), this);
            if (substitute) {
                persister.setPropertyValues(entity, state);
            }
        }
        if (persister.isIdentifierAssignedByInsert()) {
            return persister.insertReactive(state, entity, this).thenAccept(generatedId -> assignIdIfNecessary(entity, generatedId, persister, this));
        } else {
            id = assignIdIfNecessary(id, entity, persister, this);
            persister.setIdentifier(entity, id, this);
            return persister.insertReactive(id, state, entity, this);
        }
    });
}
Also used : ReactiveEntityPersister(org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister)

Aggregations

ReactiveEntityPersister (org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister)15 Serializable (java.io.Serializable)11 EntityPersister (org.hibernate.persister.entity.EntityPersister)10 CompletionStage (java.util.concurrent.CompletionStage)8 HibernateException (org.hibernate.HibernateException)8 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)8 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)8 EntityKey (org.hibernate.engine.spi.EntityKey)6 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)6 CompletionStages.completedFuture (org.hibernate.reactive.util.impl.CompletionStages.completedFuture)6 MethodHandles (java.lang.invoke.MethodHandles)5 LockMode (org.hibernate.LockMode)5 Log (org.hibernate.reactive.logging.impl.Log)5 LoggerFactory (org.hibernate.reactive.logging.impl.LoggerFactory)5 LockOptions (org.hibernate.LockOptions)4 StatisticsImplementor (org.hibernate.stat.spi.StatisticsImplementor)4 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 EntityGraph (javax.persistence.EntityGraph)3 Tuple (javax.persistence.Tuple)3