Search in sources :

Example 1 with EntityFetch

use of org.hibernate.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.

the class LoadQueryJoinAndFetchProcessor method processFetch.

private void processFetch(SelectStatementBuilder selectStatementBuilder, FetchSource fetchSource, Fetch fetch, ReaderCollector readerCollector, FetchStatsImpl fetchStats) {
    // process fetch even if it is not join fetched
    if (EntityFetch.class.isInstance(fetch)) {
        final EntityFetch entityFetch = (EntityFetch) fetch;
        processEntityFetch(selectStatementBuilder, fetchSource, entityFetch, readerCollector, fetchStats);
    } else if (CollectionAttributeFetch.class.isInstance(fetch)) {
        final CollectionAttributeFetch collectionFetch = (CollectionAttributeFetch) fetch;
        processCollectionFetch(selectStatementBuilder, fetchSource, collectionFetch, readerCollector, fetchStats);
    } else {
        // but do still need to visit their fetches...
        if (FetchSource.class.isInstance(fetch)) {
            processFetches((FetchSource) fetch, selectStatementBuilder, readerCollector, fetchStats);
        }
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) FetchSource(org.hibernate.loader.plan.spi.FetchSource)

Example 2 with EntityFetch

use of org.hibernate.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.

the class EntityReferenceInitializerImpl method handleMissingIdentifier.

private void handleMissingIdentifier(ResultSetProcessingContext context) {
    if (EntityFetch.class.isInstance(entityReference)) {
        final EntityFetch fetch = (EntityFetch) entityReference;
        final EntityType fetchedType = fetch.getFetchedType();
        if (!fetchedType.isOneToOne()) {
            return;
        }
        final EntityReferenceProcessingState fetchOwnerState = context.getOwnerProcessingState(fetch);
        if (fetchOwnerState == null) {
            throw new IllegalStateException("Could not locate fetch owner state");
        }
        final EntityKey ownerEntityKey = fetchOwnerState.getEntityKey();
        if (ownerEntityKey != null) {
            context.getSession().getPersistenceContext().addNullProperty(ownerEntityKey, fetchedType.getPropertyName());
        }
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityType(org.hibernate.type.EntityType) EntityKey(org.hibernate.engine.spi.EntityKey) EntityReferenceProcessingState(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessingContext.EntityReferenceProcessingState)

Example 3 with EntityFetch

use of org.hibernate.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.

the class ResultSetProcessingContextImpl method getProcessingState.

@Override
public EntityReferenceProcessingState getProcessingState(final EntityReference entityReference) {
    if (identifierResolutionContextMap == null) {
        identifierResolutionContextMap = new IdentityHashMap<>();
    }
    EntityReferenceProcessingState context = identifierResolutionContextMap.get(entityReference);
    if (context == null) {
        context = new EntityReferenceProcessingState() {

            private boolean wasMissingIdentifier;

            private Object identifierHydratedForm;

            private EntityKey entityKey;

            private Object[] hydratedState;

            private Object entityInstance;

            @Override
            public EntityReference getEntityReference() {
                return entityReference;
            }

            @Override
            public void registerMissingIdentifier() {
                if (!EntityFetch.class.isInstance(entityReference)) {
                    throw new IllegalStateException("Missing return row identifier");
                }
                ResultSetProcessingContextImpl.this.registerNonExists((EntityFetch) entityReference);
                wasMissingIdentifier = true;
            }

            @Override
            public boolean isMissingIdentifier() {
                return wasMissingIdentifier;
            }

            @Override
            public void registerIdentifierHydratedForm(Object identifierHydratedForm) {
                this.identifierHydratedForm = identifierHydratedForm;
            }

            @Override
            public Object getIdentifierHydratedForm() {
                return identifierHydratedForm;
            }

            @Override
            public void registerEntityKey(EntityKey entityKey) {
                this.entityKey = entityKey;
            }

            @Override
            public EntityKey getEntityKey() {
                return entityKey;
            }

            @Override
            public void registerHydratedState(Object[] hydratedState) {
                this.hydratedState = hydratedState;
            }

            @Override
            public Object[] getHydratedState() {
                return hydratedState;
            }

            @Override
            public void registerEntityInstance(Object entityInstance) {
                this.entityInstance = entityInstance;
            }

            @Override
            public Object getEntityInstance() {
                return entityInstance;
            }
        };
        identifierResolutionContextMap.put(entityReference, context);
    }
    return context;
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityKey(org.hibernate.engine.spi.EntityKey) EntityReference(org.hibernate.loader.plan.spi.EntityReference)

Example 4 with EntityFetch

use of org.hibernate.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.

the class LoadQueryJoinAndFetchProcessor method processEntityFetch.

private void processEntityFetch(SelectStatementBuilder selectStatementBuilder, FetchSource fetchSource, EntityFetch fetch, ReaderCollector readerCollector, FetchStatsImpl fetchStats) {
    // todo : still need to think through expressing bi-directionality in the new model...
    // if ( BidirectionalEntityFetch.class.isInstance( fetch ) ) {
    // log.tracef( "Skipping bi-directional entity fetch [%s]", fetch );
    // return;
    // }
    fetchStats.processingFetch(fetch);
    if (!FetchStrategyHelper.isJoinFetched(fetch.getFetchStrategy())) {
        // not join fetched, so nothing else to do
        return;
    }
    // First write out the SQL SELECT fragments
    final Joinable joinable = (Joinable) fetch.getEntityPersister();
    EntityReferenceAliases aliases = aliasResolutionContext.resolveEntityReferenceAliases(fetch.getQuerySpaceUid());
    // the null arguments here relate to many-to-many fetches
    selectStatementBuilder.appendSelectClauseFragment(joinable.selectFragment(null, null, aliases.getTableAlias(), aliases.getColumnAliases().getSuffix(), null, true));
    // process its identifier fetches first (building EntityReferenceInitializers for them if needed)
    if (fetch.getIdentifierDescription().hasFetches()) {
        final FetchSource entityIdentifierAsFetchSource = (FetchSource) fetch.getIdentifierDescription();
        for (Fetch identifierFetch : entityIdentifierAsFetchSource.getFetches()) {
            processFetch(selectStatementBuilder, fetch, identifierFetch, readerCollector, fetchStats);
        }
    }
    // build an EntityReferenceInitializers for the incoming fetch itself
    readerCollector.add(new EntityReferenceInitializerImpl(fetch, aliases));
    // then visit each of our (non-identifier) fetches
    processFetches(fetch, selectStatementBuilder, readerCollector, fetchStats);
}
Also used : Fetch(org.hibernate.loader.plan.spi.Fetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) FetchSource(org.hibernate.loader.plan.spi.FetchSource) EntityReferenceAliases(org.hibernate.loader.plan.exec.spi.EntityReferenceAliases) Joinable(org.hibernate.persister.entity.Joinable) EntityReferenceInitializerImpl(org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl)

Example 5 with EntityFetch

use of org.hibernate.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.

the class AbstractRowReader method resolveEntityKey.

private void resolveEntityKey(ResultSet resultSet, ResultSetProcessingContextImpl context, FetchSource fetchSource) throws SQLException {
    // Resolve any bidirectional entity references first.
    for (BidirectionalEntityReference bidirectionalEntityReference : fetchSource.getBidirectionalEntityReferences()) {
        final EntityReferenceInitializer targetEntityReferenceInitializer = entityInitializerByEntityReference.get(bidirectionalEntityReference.getTargetEntityReference());
        resolveEntityKey(resultSet, context, targetEntityReferenceInitializer);
        targetEntityReferenceInitializer.hydrateEntityState(resultSet, context);
    }
    for (Fetch fetch : fetchSource.getFetches()) {
        if (EntityFetch.class.isInstance(fetch)) {
            final EntityFetch entityFetch = (EntityFetch) fetch;
            final EntityReferenceInitializer entityReferenceInitializer = entityInitializerByEntityReference.get(entityFetch);
            if (entityReferenceInitializer != null) {
                resolveEntityKey(resultSet, context, entityReferenceInitializer);
                entityReferenceInitializer.hydrateEntityState(resultSet, context);
            }
        } else if (CompositeFetch.class.isInstance(fetch)) {
            resolveEntityKey(resultSet, context, (CompositeFetch) fetch);
        }
    }
}
Also used : CompositeFetch(org.hibernate.loader.plan.spi.CompositeFetch) Fetch(org.hibernate.loader.plan.spi.Fetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) CompositeFetch(org.hibernate.loader.plan.spi.CompositeFetch) BidirectionalEntityReference(org.hibernate.loader.plan.spi.BidirectionalEntityReference) EntityReferenceInitializer(org.hibernate.loader.plan.exec.process.spi.EntityReferenceInitializer)

Aggregations

EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)12 CollectionAttributeFetch (org.hibernate.loader.plan.spi.CollectionAttributeFetch)5 Fetch (org.hibernate.loader.plan.spi.Fetch)4 EntityReturn (org.hibernate.loader.plan.spi.EntityReturn)3 FetchSource (org.hibernate.loader.plan.spi.FetchSource)3 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 Test (org.junit.Test)3 EntityKey (org.hibernate.engine.spi.EntityKey)2 AliasResolutionContextImpl (org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl)2 BidirectionalEntityReference (org.hibernate.loader.plan.spi.BidirectionalEntityReference)2 CollectionReturn (org.hibernate.loader.plan.spi.CollectionReturn)2 CompositeFetch (org.hibernate.loader.plan.spi.CompositeFetch)2 Return (org.hibernate.loader.plan.spi.Return)2 EntityType (org.hibernate.type.EntityType)2 Serializable (java.io.Serializable)1 SQLException (java.sql.SQLException)1 Configuration (org.hibernate.cfg.Configuration)1 FetchStrategy (org.hibernate.engine.FetchStrategy)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1