Search in sources :

Example 1 with AbstractEntityInitializer

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);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) Type(org.hibernate.type.Type) EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) AbstractEntityInitializer(org.hibernate.sql.results.graph.entity.AbstractEntityInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry)

Aggregations

EntityKey (org.hibernate.engine.spi.EntityKey)1 EntityUniqueKey (org.hibernate.engine.spi.EntityUniqueKey)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 HibernateProxy (org.hibernate.proxy.HibernateProxy)1 AbstractEntityInitializer (org.hibernate.sql.results.graph.entity.AbstractEntityInitializer)1 LoadingEntityEntry (org.hibernate.sql.results.graph.entity.LoadingEntityEntry)1 Type (org.hibernate.type.Type)1