Search in sources :

Example 1 with EffectiveEntityGraph

use of org.hibernate.engine.spi.EffectiveEntityGraph in project hibernate-orm by hibernate.

the class SessionImpl method internalLoad.

@Override
public Object internalLoad(String entityName, Object id, boolean eager, boolean nullable) {
    final EffectiveEntityGraph effectiveEntityGraph = getLoadQueryInfluencers().getEffectiveEntityGraph();
    final GraphSemantic semantic = effectiveEntityGraph.getSemantic();
    final RootGraphImplementor<?> graph = effectiveEntityGraph.getGraph();
    boolean clearedEffectiveGraph = false;
    if (semantic != null) {
        if (!graph.appliesTo(entityName)) {
            log.debug("Clearing effective entity graph for subsequent-select");
            clearedEffectiveGraph = true;
            effectiveEntityGraph.clear();
        }
    }
    try {
        final LoadType type;
        if (nullable) {
            type = LoadEventListener.INTERNAL_LOAD_NULLABLE;
        } else {
            type = eager ? LoadEventListener.INTERNAL_LOAD_EAGER : LoadEventListener.INTERNAL_LOAD_LAZY;
        }
        LoadEvent event = loadEvent;
        loadEvent = null;
        event = recycleEventInstance(event, id, entityName);
        fireLoadNoChecks(event, type);
        Object result = event.getResult();
        if (!nullable) {
            UnresolvableObjectException.throwIfNull(result, id, entityName);
        }
        if (loadEvent == null) {
            event.setEntityClassName(null);
            event.setEntityId(null);
            event.setInstanceToLoad(null);
            event.setResult(null);
            loadEvent = event;
        }
        return result;
    } finally {
        if (clearedEffectiveGraph) {
            effectiveEntityGraph.applyGraph(graph, semantic);
        }
    }
}
Also used : LoadEvent(org.hibernate.event.spi.LoadEvent) LoadType(org.hibernate.event.spi.LoadEventListener.LoadType) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) GraphSemantic(org.hibernate.graph.GraphSemantic)

Example 2 with EffectiveEntityGraph

use of org.hibernate.engine.spi.EffectiveEntityGraph in project hibernate-orm by hibernate.

the class LoaderSelectBuilder method determineGraphTraversalState.

private static EntityGraphTraversalState determineGraphTraversalState(LoadQueryInfluencers loadQueryInfluencers) {
    if (loadQueryInfluencers != null) {
        final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
        if (effectiveEntityGraph != null) {
            final GraphSemantic graphSemantic = effectiveEntityGraph.getSemantic();
            final RootGraphImplementor rootGraphImplementor = effectiveEntityGraph.getGraph();
            if (graphSemantic != null && rootGraphImplementor != null) {
                return new StandardEntityGraphTraversalStateImpl(graphSemantic, rootGraphImplementor);
            }
        }
    }
    return null;
}
Also used : RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) GraphSemantic(org.hibernate.graph.GraphSemantic) StandardEntityGraphTraversalStateImpl(org.hibernate.sql.results.internal.StandardEntityGraphTraversalStateImpl)

Example 3 with EffectiveEntityGraph

use of org.hibernate.engine.spi.EffectiveEntityGraph in project hibernate-orm by hibernate.

the class NaturalIdMultiLoadAccessStandard method multiLoad.

@Override
@SuppressWarnings("unchecked")
public List<T> multiLoad(Object... ids) {
    final CacheMode sessionCacheMode = session.getCacheMode();
    boolean cacheModeChanged = false;
    if (cacheMode != null) {
        // todo : account for "conceptually equal"
        if (cacheMode != sessionCacheMode) {
            session.setCacheMode(cacheMode);
            cacheModeChanged = true;
        }
    }
    session.autoFlushIfRequired((Set) CollectionHelper.setOf(entityDescriptor.getQuerySpaces()));
    final LoadQueryInfluencers loadQueryInfluencers = session.getLoadQueryInfluencers();
    try {
        final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
        final GraphSemantic initialGraphSemantic = effectiveEntityGraph.getSemantic();
        final RootGraphImplementor<?> initialGraph = effectiveEntityGraph.getGraph();
        final boolean hadInitialGraph = initialGraphSemantic != null;
        if (graphSemantic != null) {
            if (rootGraph == null) {
                throw new IllegalArgumentException("Graph semantic specified, but no RootGraph was supplied");
            }
            effectiveEntityGraph.applyGraph(rootGraph, graphSemantic);
        }
        try {
            return (List<T>) entityDescriptor.getMultiNaturalIdLoader().multiLoad(ids, this, session);
        } finally {
            if (graphSemantic != null) {
                if (hadInitialGraph) {
                    effectiveEntityGraph.applyGraph(initialGraph, initialGraphSemantic);
                } else {
                    effectiveEntityGraph.clear();
                }
            }
        }
    } finally {
        if (cacheModeChanged) {
            // change it back
            session.setCacheMode(sessionCacheMode);
        }
    }
}
Also used : LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) CacheMode(org.hibernate.CacheMode) List(java.util.List) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) GraphSemantic(org.hibernate.graph.GraphSemantic)

Example 4 with EffectiveEntityGraph

use of org.hibernate.engine.spi.EffectiveEntityGraph in project hibernate-orm by hibernate.

the class EntityGraphLoadPlanBuilderTest method buildSqlSelectAst.

private <T> SelectStatement buildSqlSelectAst(Class<T> entityType, RootGraphImplementor<T> entityGraph, GraphSemantic mode, SessionFactoryScope scope) {
    final EntityPersister entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityType);
    final LoadQueryInfluencers loadQueryInfluencers = new LoadQueryInfluencers(scope.getSessionFactory());
    final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
    effectiveEntityGraph.applyGraph(entityGraph, mode);
    return LoaderSelectBuilder.createSelect(entityDescriptor, null, entityDescriptor.getIdentifierMapping(), null, 1, loadQueryInfluencers, LockOptions.READ, jdbcParameter -> {
    }, scope.getSessionFactory());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph)

Example 5 with EffectiveEntityGraph

use of org.hibernate.engine.spi.EffectiveEntityGraph in project hibernate-reactive by hibernate.

the class ReactiveSessionImpl method reactiveInternalLoad.

@Override
public CompletionStage<Object> reactiveInternalLoad(String entityName, Serializable id, boolean eager, boolean nullable) {
    final EffectiveEntityGraph effectiveEntityGraph = getLoadQueryInfluencers().getEffectiveEntityGraph();
    final GraphSemantic semantic = effectiveEntityGraph.getSemantic();
    final RootGraphImplementor<?> graph = effectiveEntityGraph.getGraph();
    boolean clearedEffectiveGraph;
    if (semantic == null || graph.appliesTo(entityName)) {
        clearedEffectiveGraph = false;
    } else {
        // log.debug( "Clearing effective entity graph for subsequent-select" );
        clearedEffectiveGraph = true;
        effectiveEntityGraph.clear();
    }
    final LoadEventListener.LoadType type;
    if (nullable) {
        type = LoadEventListener.INTERNAL_LOAD_NULLABLE;
    } else {
        type = eager ? LoadEventListener.INTERNAL_LOAD_EAGER : LoadEventListener.INTERNAL_LOAD_LAZY;
    }
    threadCheck();
    LoadEvent event = new LoadEvent(id, entityName, true, this, getReadOnlyFromLoadQueryInfluencers());
    return fireLoadNoChecks(event, type).thenApply(v -> {
        Object result = event.getResult();
        if (!nullable) {
            UnresolvableObjectException.throwIfNull(result, id, entityName);
        }
        return result;
    }).whenComplete((v, x) -> {
        if (clearedEffectiveGraph) {
            effectiveEntityGraph.applyGraph(graph, semantic);
        }
    });
}
Also used : LoadEventListener(org.hibernate.event.spi.LoadEventListener) ReactiveLoadEventListener(org.hibernate.reactive.event.ReactiveLoadEventListener) CompletionStages.completedFuture(org.hibernate.reactive.util.impl.CompletionStages.completedFuture) BatchingConnection(org.hibernate.reactive.pool.BatchingConnection) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) ExceptionConverter(org.hibernate.engine.spi.ExceptionConverter) NativeQueryTupleTransformer(org.hibernate.jpa.spi.NativeQueryTupleTransformer) EventSource(org.hibernate.event.spi.EventSource) Log(org.hibernate.reactive.logging.impl.Log) CompletionStages.returnNullorRethrow(org.hibernate.reactive.util.impl.CompletionStages.returnNullorRethrow) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) LoggerFactory(org.hibernate.reactive.logging.impl.LoggerFactory) InitializeCollectionEvent(org.hibernate.event.spi.InitializeCollectionEvent) ReactivePersistEventListener(org.hibernate.reactive.event.ReactivePersistEventListener) MergeContext(org.hibernate.event.internal.MergeContext) SQLCustomQuery(org.hibernate.loader.custom.sql.SQLCustomQuery) Map(java.util.Map) HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) InternalStateAssertions.assertUseOnEventLoop(org.hibernate.reactive.common.InternalStateAssertions.assertUseOnEventLoop) CompletionStages.voidFuture(org.hibernate.reactive.util.impl.CompletionStages.voidFuture) ResolveNaturalIdEvent(org.hibernate.event.spi.ResolveNaturalIdEvent) LockOptions(org.hibernate.LockOptions) MethodHandles(java.lang.invoke.MethodHandles) FlushEvent(org.hibernate.event.spi.FlushEvent) Set(java.util.Set) RefreshEvent(org.hibernate.event.spi.RefreshEvent) ResultSetMapping(org.hibernate.reactive.common.ResultSetMapping) CacheMode(org.hibernate.CacheMode) Serializable(java.io.Serializable) CompletionStage(java.util.concurrent.CompletionStage) Dialect(org.hibernate.dialect.Dialect) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) AutoFlushEvent(org.hibernate.event.spi.AutoFlushEvent) ReactiveEntityPersister(org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister) HibernateException(org.hibernate.HibernateException) CompletionStages(org.hibernate.reactive.util.impl.CompletionStages) TypeMismatchException(org.hibernate.TypeMismatchException) RootGraph(org.hibernate.graph.RootGraph) SessionUtil.checkEntityFound(org.hibernate.reactive.session.impl.SessionUtil.checkEntityFound) Supplier(java.util.function.Supplier) RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) ReactiveResolveNaturalIdEventListener(org.hibernate.reactive.event.ReactiveResolveNaturalIdEventListener) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) Status(org.hibernate.engine.spi.Status) ReactiveSession(org.hibernate.reactive.session.ReactiveSession) Tuple(javax.persistence.Tuple) ReactiveFlushEventListener(org.hibernate.reactive.event.ReactiveFlushEventListener) CompletionStages.rethrow(org.hibernate.reactive.util.impl.CompletionStages.rethrow) EntityKey(org.hibernate.engine.spi.EntityKey) CriteriaQueryOptions(org.hibernate.reactive.session.CriteriaQueryOptions) ReactiveMergeEventListener(org.hibernate.reactive.event.ReactiveMergeEventListener) StatefulPersistenceContext(org.hibernate.engine.internal.StatefulPersistenceContext) SessionImpl(org.hibernate.internal.SessionImpl) SessionCreationOptions(org.hibernate.internal.SessionCreationOptions) ReactiveDeleteEventListener(org.hibernate.reactive.event.ReactiveDeleteEventListener) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) NativeSQLQuerySpecification(org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification) LoadEvent(org.hibernate.event.spi.LoadEvent) ReactiveConnection(org.hibernate.reactive.pool.ReactiveConnection) EntityPersister(org.hibernate.persister.entity.EntityPersister) GraphSemantic(org.hibernate.graph.GraphSemantic) MessageHelper(org.hibernate.pretty.MessageHelper) MergeEvent(org.hibernate.event.spi.MergeEvent) DefaultReactiveAutoFlushEventListener(org.hibernate.reactive.event.impl.DefaultReactiveAutoFlushEventListener) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) ObjectDeletedException(org.hibernate.ObjectDeletedException) ReactiveCustomLoader(org.hibernate.reactive.loader.custom.impl.ReactiveCustomLoader) HibernateProxy(org.hibernate.proxy.HibernateProxy) JDBCException(org.hibernate.JDBCException) LockEvent(org.hibernate.event.spi.LockEvent) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) ReactiveNativeQuery(org.hibernate.reactive.session.ReactiveNativeQuery) EntityGraph(javax.persistence.EntityGraph) ParameterMetadata(org.hibernate.query.ParameterMetadata) Attribute(javax.persistence.metamodel.Attribute) CompletionException(java.util.concurrent.CompletionException) LoadEventListener(org.hibernate.event.spi.LoadEventListener) List(java.util.List) ReactiveLockEventListener(org.hibernate.reactive.event.ReactiveLockEventListener) ReactiveRefreshEventListener(org.hibernate.reactive.event.ReactiveRefreshEventListener) MappingException(org.hibernate.MappingException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ReactiveLoadEventListener(org.hibernate.reactive.event.ReactiveLoadEventListener) Criteria(org.hibernate.reactive.session.Criteria) ReactiveActionQueue(org.hibernate.reactive.engine.ReactiveActionQueue) NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) INVALID_NATURAL_ID_REFERENCE(org.hibernate.engine.spi.PersistenceContext.NaturalIdHelper.INVALID_NATURAL_ID_REFERENCE) PersistEvent(org.hibernate.event.spi.PersistEvent) InternalStateAssertions(org.hibernate.reactive.common.InternalStateAssertions) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) CriteriaQueryTupleTransformer(org.hibernate.jpa.spi.CriteriaQueryTupleTransformer) MultiLoadOptions(org.hibernate.persister.entity.MultiLoadOptions) ReactiveQuery(org.hibernate.reactive.session.ReactiveQuery) EntityNotFoundException(javax.persistence.EntityNotFoundException) BulkOperationCleanupAction(org.hibernate.action.internal.BulkOperationCleanupAction) DeleteEvent(org.hibernate.event.spi.DeleteEvent) QueryParameters(org.hibernate.engine.spi.QueryParameters) LockMode(org.hibernate.LockMode) LazyInitializer(org.hibernate.proxy.LazyInitializer) FlushMode(org.hibernate.FlushMode) CompletionStages.returnOrRethrow(org.hibernate.reactive.util.impl.CompletionStages.returnOrRethrow) ReactivePersistenceContextAdapter(org.hibernate.reactive.engine.impl.ReactivePersistenceContextAdapter) DefaultReactiveInitializeCollectionEventListener(org.hibernate.reactive.event.impl.DefaultReactiveInitializeCollectionEventListener) EntityEntry(org.hibernate.engine.spi.EntityEntry) LoadEvent(org.hibernate.event.spi.LoadEvent) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) GraphSemantic(org.hibernate.graph.GraphSemantic)

Aggregations

EffectiveEntityGraph (org.hibernate.engine.spi.EffectiveEntityGraph)5 GraphSemantic (org.hibernate.graph.GraphSemantic)4 List (java.util.List)2 CacheMode (org.hibernate.CacheMode)2 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)2 RootGraphImplementor (org.hibernate.graph.spi.RootGraphImplementor)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Serializable (java.io.Serializable)1 MethodHandles (java.lang.invoke.MethodHandles)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletionException (java.util.concurrent.CompletionException)1 CompletionStage (java.util.concurrent.CompletionStage)1 Supplier (java.util.function.Supplier)1 EntityGraph (javax.persistence.EntityGraph)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 Tuple (javax.persistence.Tuple)1 Attribute (javax.persistence.metamodel.Attribute)1 FlushMode (org.hibernate.FlushMode)1 HibernateException (org.hibernate.HibernateException)1