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