Search in sources :

Example 1 with Initializer

use of org.hibernate.sql.results.graph.Initializer in project hibernate-orm by hibernate.

the class AbstractEmbeddableInitializer method determineParentInstance.

private Object determineParentInstance(RowProcessingState processingState) {
    // or at least the fetch-parent of the collection could get passed.
    if (fetchParentAccess != null) {
        // the embeddable being initialized is a fetch, so use the fetchParentAccess
        // to get the parent reference
        // 
        // at the moment, this uses the legacy behavior of injecting the "first
        // containing entity" as the parent.  however,
        // todo (6.x) - allow injection of containing composite as parent if
        // it is the direct parent
        final FetchParentAccess firstEntityDescriptorAccess = fetchParentAccess.findFirstEntityDescriptorAccess();
        return firstEntityDescriptorAccess.getInitializedInstance();
    }
    // Otherwise, fallback to determining the parent-initializer by path
    // todo (6.0) - this is the part that should be "subsumed" based on the
    // comment above
    final NavigablePath parentPath = navigablePath.getParent();
    if (parentPath == null) {
        return null;
    }
    final Initializer parentInitializer = processingState.resolveInitializer(parentPath);
    if (parentInitializer instanceof CollectionInitializer) {
        return ((CollectionInitializer) parentInitializer).getCollectionInstance().getOwner();
    }
    if (parentInitializer instanceof EntityInitializer) {
        return ((EntityInitializer) parentInitializer).getEntityInstance();
    }
    throw new NotYetImplementedFor6Exception(getClass());
}
Also used : AbstractFetchParentAccess(org.hibernate.sql.results.graph.AbstractFetchParentAccess) FetchParentAccess(org.hibernate.sql.results.graph.FetchParentAccess) NavigablePath(org.hibernate.query.spi.NavigablePath) EntityInitializer(org.hibernate.sql.results.graph.entity.EntityInitializer) CollectionInitializer(org.hibernate.sql.results.graph.collection.CollectionInitializer) Initializer(org.hibernate.sql.results.graph.Initializer) EntityInitializer(org.hibernate.sql.results.graph.entity.EntityInitializer) CollectionInitializer(org.hibernate.sql.results.graph.collection.CollectionInitializer) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception)

Example 2 with Initializer

use of org.hibernate.sql.results.graph.Initializer in project hibernate-orm by hibernate.

the class BatchEntitySelectFetchInitializer method initializeInstance.

@Override
public void initializeInstance(RowProcessingState rowProcessingState) {
    if (isInitialized) {
        return;
    }
    if (!isAttributeAssignableToConcreteDescriptor()) {
        return;
    }
    final Object entityIdentifier = identifierAssembler.assemble(rowProcessingState);
    if (entityIdentifier == null) {
        return;
    }
    entityKey = new EntityKey(entityIdentifier, concreteDescriptor);
    final PersistenceContext persistenceContext = rowProcessingState.getSession().getPersistenceContextInternal();
    entityInstance = persistenceContext.getEntity(entityKey);
    if (entityInstance != null) {
        return;
    }
    Initializer initializer = rowProcessingState.getJdbcValuesSourceProcessingState().findInitializer(entityKey);
    if (initializer != null) {
        if (EntityLoadingLogger.DEBUG_ENABLED) {
            EntityLoadingLogger.LOGGER.debugf("(%s) Found an initializer for entity (%s) : %s", CONCRETE_NAME, toLoggableString(getNavigablePath(), entityIdentifier), entityIdentifier);
        }
        initializer.resolveInstance(rowProcessingState);
        entityInstance = initializer.getInitializedInstance();
        // EARLY EXIT!!!
        return;
    }
    final LoadingEntityEntry existingLoadingEntry = rowProcessingState.getSession().getPersistenceContext().getLoadContexts().findLoadingEntityEntry(entityKey);
    if (existingLoadingEntry != null) {
        if (existingLoadingEntry.getEntityInitializer() != this) {
            // the entity is already being loaded elsewhere
            if (EntityLoadingLogger.DEBUG_ENABLED) {
                EntityLoadingLogger.LOGGER.debugf("(%s) Entity [%s] being loaded by another initializer [%s] - skipping processing", CONCRETE_NAME, toLoggableString(getNavigablePath(), entityIdentifier), existingLoadingEntry.getEntityInitializer());
            }
            this.entityInstance = existingLoadingEntry.getEntityInstance();
            // EARLY EXIT!!!
            return;
        }
    }
    persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey(entityKey);
    toBatchLoad.put(entityKey, parentAccess.getInitializedInstance());
    isInitialized = true;
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) EntityInitializer(org.hibernate.sql.results.graph.entity.EntityInitializer) Initializer(org.hibernate.sql.results.graph.Initializer) AbstractEntityInitializer(org.hibernate.sql.results.graph.entity.AbstractEntityInitializer) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry)

Example 3 with Initializer

use of org.hibernate.sql.results.graph.Initializer in project hibernate-orm by hibernate.

the class EntitySelectFetchInitializer method initializeInstance.

@Override
public void initializeInstance(RowProcessingState rowProcessingState) {
    if (entityInstance != null || isInitialized) {
        return;
    }
    if (!isAttributeAssignableToConcreteDescriptor()) {
        return;
    }
    final Object entityIdentifier = identifierAssembler.assemble(rowProcessingState);
    if (entityIdentifier == null) {
        isInitialized = true;
        return;
    }
    if (EntityLoadingLogger.TRACE_ENABLED) {
        EntityLoadingLogger.LOGGER.tracef("(%s) Beginning Initializer#resolveInstance process for entity (%s) : %s", StringHelper.collapse(this.getClass().getName()), getNavigablePath(), entityIdentifier);
    }
    final SharedSessionContractImplementor session = rowProcessingState.getSession();
    final String entityName = concreteDescriptor.getEntityName();
    final EntityKey entityKey = new EntityKey(entityIdentifier, concreteDescriptor);
    final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
    entityInstance = persistenceContext.getEntity(entityKey);
    if (entityInstance != null) {
        isInitialized = true;
        return;
    }
    Initializer initializer = rowProcessingState.getJdbcValuesSourceProcessingState().findInitializer(entityKey);
    if (initializer != null) {
        if (EntityLoadingLogger.DEBUG_ENABLED) {
            EntityLoadingLogger.LOGGER.debugf("(%s) Found an initializer for entity (%s) : %s", CONCRETE_NAME, toLoggableString(getNavigablePath(), entityIdentifier), entityIdentifier);
        }
        initializer.resolveInstance(rowProcessingState);
        entityInstance = initializer.getInitializedInstance();
        isInitialized = true;
        return;
    }
    final LoadingEntityEntry existingLoadingEntry = session.getPersistenceContext().getLoadContexts().findLoadingEntityEntry(entityKey);
    if (existingLoadingEntry != null) {
        if (EntityLoadingLogger.DEBUG_ENABLED) {
            EntityLoadingLogger.LOGGER.debugf("(%s) Found existing loading entry [%s] - using loading instance", CONCRETE_NAME, toLoggableString(getNavigablePath(), entityIdentifier));
        }
        this.entityInstance = existingLoadingEntry.getEntityInstance();
        if (existingLoadingEntry.getEntityInitializer() != this) {
            // the entity is already being loaded elsewhere
            if (EntityLoadingLogger.DEBUG_ENABLED) {
                EntityLoadingLogger.LOGGER.debugf("(%s) Entity [%s] being loaded by another initializer [%s] - skipping processing", CONCRETE_NAME, toLoggableString(getNavigablePath(), entityIdentifier), existingLoadingEntry.getEntityInitializer());
            }
            // EARLY EXIT!!!
            isInitialized = true;
            return;
        }
    }
    if (EntityLoadingLogger.DEBUG_ENABLED) {
        EntityLoadingLogger.LOGGER.debugf("(%s) Invoking session#internalLoad for entity (%s) : %s", CONCRETE_NAME, toLoggableString(getNavigablePath(), entityIdentifier), entityIdentifier);
    }
    entityInstance = session.internalLoad(entityName, entityIdentifier, true, referencedModelPart.isNullable() || referencedModelPart.isIgnoreNotFound());
    if (EntityLoadingLogger.DEBUG_ENABLED) {
        EntityLoadingLogger.LOGGER.debugf("(%s) Entity [%s] : %s has being loaded by session.internalLoad.", CONCRETE_NAME, toLoggableString(getNavigablePath(), entityIdentifier), entityIdentifier);
    }
    final boolean unwrapProxy = referencedModelPart.isUnwrapProxy() && isEnhancedForLazyLoading;
    if (entityInstance instanceof HibernateProxy) {
        ((HibernateProxy) entityInstance).getHibernateLazyInitializer().setUnwrap(unwrapProxy);
    }
    isInitialized = true;
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) Initializer(org.hibernate.sql.results.graph.Initializer) EntityInitializer(org.hibernate.sql.results.graph.entity.EntityInitializer) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) LoggingHelper.toLoggableString(org.hibernate.internal.log.LoggingHelper.toLoggableString) HibernateProxy(org.hibernate.proxy.HibernateProxy) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry)

Example 4 with Initializer

use of org.hibernate.sql.results.graph.Initializer in project hibernate-orm by hibernate.

the class AbstractEmbeddableInitializer method initializeInstance.

@Override
public void initializeInstance(RowProcessingState processingState) {
    EmbeddableLoadingLogger.INSTANCE.debugf("Initializing composite instance [%s]", navigablePath);
    if (compositeInstance == NULL_MARKER) {
        // we already know it is null
        return;
    }
    if (!usesStandardInstatiation) {
        // we have a custom instantiator
        if (compositeInstance != null) {
            return;
        }
    }
    stateInjected = false;
    extractRowState(processingState);
    prepareCompositeInstance(processingState);
    handleParentInjection(processingState);
    if (compositeInstance != NULL_MARKER) {
        notifyResolutionListeners(compositeInstance);
        if (compositeInstance instanceof HibernateProxy) {
            final Initializer parentInitializer = processingState.resolveInitializer(navigablePath.getParent());
            if (parentInitializer != this) {
                ((FetchParentAccess) parentInitializer).registerResolutionListener((entity) -> {
                    representationEmbeddable.setValues(entity, rowState);
                    stateInjected = true;
                });
            } else {
                // At this point, createEmptyCompositesEnabled is always true, so we generate
                // the composite instance.
                // 
                // NOTE: `valuesAccess` is set to null to indicate that all values are null,
                // as opposed to returning the all-null value array.  the instantiator
                // interprets that as the values are not known or were all null.
                final Object target = representationStrategy.getInstantiator().instantiate(this, sessionFactory);
                stateInjected = true;
                ((HibernateProxy) compositeInstance).getHibernateLazyInitializer().setImplementation(target);
            }
        } else if (stateAllNull == FALSE && stateInjected != TRUE) {
            representationEmbeddable.setValues(compositeInstance, rowState);
            stateInjected = true;
        }
    }
}
Also used : AbstractFetchParentAccess(org.hibernate.sql.results.graph.AbstractFetchParentAccess) FetchParentAccess(org.hibernate.sql.results.graph.FetchParentAccess) EntityInitializer(org.hibernate.sql.results.graph.entity.EntityInitializer) CollectionInitializer(org.hibernate.sql.results.graph.collection.CollectionInitializer) Initializer(org.hibernate.sql.results.graph.Initializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 5 with Initializer

use of org.hibernate.sql.results.graph.Initializer in project hibernate-orm by hibernate.

the class ResultsHelper method createRowReader.

public static <R> RowReader<R> createRowReader(ExecutionContext executionContext, LockOptions lockOptions, RowTransformer<R> rowTransformer, JdbcValues jdbcValues) {
    final Map<NavigablePath, Initializer> initializerMap = new LinkedHashMap<>();
    final List<Initializer> initializers = new ArrayList<>();
    final SessionFactoryImplementor sessionFactory = executionContext.getSession().getFactory();
    final List<DomainResultAssembler<?>> assemblers = jdbcValues.getValuesMapping().resolveAssemblers(new AssemblerCreationState() {

        @Override
        public LockMode determineEffectiveLockMode(String identificationVariable) {
            return lockOptions.getEffectiveLockMode(identificationVariable);
        }

        @Override
        public Initializer resolveInitializer(NavigablePath navigablePath, ModelPart fetchedModelPart, Supplier<Initializer> producer) {
            final Initializer existing = initializerMap.get(navigablePath);
            if (existing != null) {
                if (fetchedModelPart.getNavigableRole().equals(existing.getInitializedPart().getNavigableRole())) {
                    ResultsLogger.LOGGER.tracef("Returning previously-registered initializer : %s", existing);
                    return existing;
                }
            }
            final Initializer initializer = producer.get();
            ResultsLogger.LOGGER.tracef("Registering initializer : %s", initializer);
            initializerMap.put(navigablePath, initializer);
            initializers.add(initializer);
            return initializer;
        }

        @Override
        public SqlAstCreationContext getSqlAstCreationContext() {
            return sessionFactory;
        }
    });
    logInitializers(initializerMap);
    return new StandardRowReader<>(assemblers, initializers, rowTransformer);
}
Also used : NavigablePath(org.hibernate.query.spi.NavigablePath) ModelPart(org.hibernate.metamodel.mapping.ModelPart) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ArrayList(java.util.ArrayList) LockMode(org.hibernate.LockMode) AssemblerCreationState(org.hibernate.sql.results.graph.AssemblerCreationState) LinkedHashMap(java.util.LinkedHashMap) DomainResultAssembler(org.hibernate.sql.results.graph.DomainResultAssembler) Initializer(org.hibernate.sql.results.graph.Initializer) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext)

Aggregations

Initializer (org.hibernate.sql.results.graph.Initializer)5 EntityInitializer (org.hibernate.sql.results.graph.entity.EntityInitializer)4 EntityKey (org.hibernate.engine.spi.EntityKey)2 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)2 HibernateProxy (org.hibernate.proxy.HibernateProxy)2 NavigablePath (org.hibernate.query.spi.NavigablePath)2 AbstractFetchParentAccess (org.hibernate.sql.results.graph.AbstractFetchParentAccess)2 FetchParentAccess (org.hibernate.sql.results.graph.FetchParentAccess)2 CollectionInitializer (org.hibernate.sql.results.graph.collection.CollectionInitializer)2 LoadingEntityEntry (org.hibernate.sql.results.graph.entity.LoadingEntityEntry)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LockMode (org.hibernate.LockMode)1 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 LoggingHelper.toLoggableString (org.hibernate.internal.log.LoggingHelper.toLoggableString)1 ModelPart (org.hibernate.metamodel.mapping.ModelPart)1 SqlAstCreationContext (org.hibernate.sql.ast.spi.SqlAstCreationContext)1 AssemblerCreationState (org.hibernate.sql.results.graph.AssemblerCreationState)1