use of org.hibernate.sql.results.graph.entity.AbstractEntityInitializer in project hibernate-orm by hibernate.
the class EntityDelayedFetchInitializer method resolveInstance.
@Override
public void resolveInstance(RowProcessingState rowProcessingState) {
if (entityInstance != null) {
return;
}
if (!isAttributeAssignableToConcreteDescriptor()) {
return;
}
identifier = identifierAssembler.assemble(rowProcessingState);
if (identifier == null) {
// todo (6.0) : check this is the correct behaviour
entityInstance = null;
} else {
final SharedSessionContractImplementor session = rowProcessingState.getSession();
final EntityPersister concreteDescriptor = referencedModelPart.getEntityMappingType().getEntityPersister();
if (!selectByUniqueKey) {
final EntityKey entityKey = new EntityKey(identifier, concreteDescriptor);
final PersistenceContext persistenceContext = session.getPersistenceContext();
final LoadingEntityEntry loadingEntityLocally = persistenceContext.getLoadContexts().findLoadingEntityEntry(entityKey);
if (loadingEntityLocally != null) {
entityInstance = loadingEntityLocally.getEntityInstance();
}
}
if (entityInstance == null) {
if (referencedModelPart.isOptional() && parentAccess != null && parentAccess.getInitializedPart().findContainingEntityMapping().getEntityPersister().getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()) {
entityInstance = LazyPropertyInitializer.UNFETCHED_PROPERTY;
} else {
if (selectByUniqueKey) {
final String uniqueKeyPropertyName = referencedModelPart.getBidirectionalAttributeName();
final Type uniqueKeyPropertyType = (referencedModelPart.getReferencedPropertyName() == null) ? concreteDescriptor.getIdentifierType() : session.getFactory().getReferencedPropertyType(concreteDescriptor.getEntityName(), uniqueKeyPropertyName);
final EntityUniqueKey euk = new EntityUniqueKey(concreteDescriptor.getEntityName(), uniqueKeyPropertyName, identifier, uniqueKeyPropertyType, session.getFactory());
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
entityInstance = persistenceContext.getEntity(euk);
if (entityInstance == null) {
if (((AbstractEntityInitializer) this.parentAccess).getEntityDescriptor().getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()) {
return;
}
entityInstance = ((UniqueKeyLoadable) concreteDescriptor).loadByUniqueKey(uniqueKeyPropertyName, identifier, session);
// add it to the Persistence Context
if (entityInstance != null) {
persistenceContext.addEntity(euk, entityInstance);
}
}
if (entityInstance != null) {
entityInstance = persistenceContext.proxyFor(entityInstance);
}
} else {
entityInstance = session.internalLoad(concreteDescriptor.getEntityName(), identifier, false, false);
}
if (entityInstance instanceof HibernateProxy) {
((HibernateProxy) entityInstance).getHibernateLazyInitializer().setUnwrap(referencedModelPart.isUnwrapProxy() && concreteDescriptor.isInstrumented());
}
}
}
notifyResolutionListeners(entityInstance);
}
}
Aggregations