Search in sources :

Example 1 with Fetch

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

the class LoadQueryJoinAndFetchProcessor method processFetches.

public FetchStats processFetches(FetchSource fetchSource, SelectStatementBuilder selectStatementBuilder, ReaderCollector readerCollector) {
    final FetchStatsImpl fetchStats = new FetchStatsImpl();
    // what if fetchSource is a composite fetch (as it would be in the case of a key-many-to-one)?
    if (EntityReference.class.isInstance(fetchSource)) {
        final EntityReference fetchOwnerAsEntityReference = (EntityReference) fetchSource;
        if (fetchOwnerAsEntityReference.getIdentifierDescription().hasFetches()) {
            final FetchSource entityIdentifierAsFetchSource = (FetchSource) fetchOwnerAsEntityReference.getIdentifierDescription();
            for (Fetch fetch : entityIdentifierAsFetchSource.getFetches()) {
                processFetch(selectStatementBuilder, fetchSource, fetch, readerCollector, fetchStats);
            }
        }
    }
    processFetches(fetchSource, selectStatementBuilder, readerCollector, fetchStats);
    return 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) EntityReference(org.hibernate.loader.plan.spi.EntityReference)

Example 2 with Fetch

use of org.hibernate.loader.plan.spi.Fetch 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 3 with Fetch

use of org.hibernate.loader.plan.spi.Fetch 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)

Example 4 with Fetch

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

the class LoadPlanBuilderTest method testSimpleBuild.

@Test
public void testSimpleBuild() {
    EntityPersister ep = (EntityPersister) sessionFactory().getClassMetadata(Message.class);
    FetchStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new FetchStyleLoadPlanBuildingAssociationVisitationStrategy(sessionFactory(), LoadQueryInfluencers.NONE, LockMode.NONE);
    LoadPlan plan = MetamodelDrivenLoadPlanBuilder.buildRootEntityLoadPlan(strategy, ep);
    assertFalse(plan.hasAnyScalarReturns());
    assertEquals(1, plan.getReturns().size());
    Return rtn = plan.getReturns().get(0);
    EntityReturn entityReturn = ExtraAssertions.assertTyping(EntityReturn.class, rtn);
    assertNotNull(entityReturn.getFetches());
    assertEquals(1, entityReturn.getFetches().length);
    Fetch fetch = entityReturn.getFetches()[0];
    EntityFetch entityFetch = ExtraAssertions.assertTyping(EntityFetch.class, fetch);
    assertNotNull(entityFetch.getFetches());
    assertEquals(0, entityFetch.getFetches().length);
    LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sessionFactory()));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Fetch(org.hibernate.loader.plan.spi.Fetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) CollectionReturn(org.hibernate.loader.plan.spi.CollectionReturn) Return(org.hibernate.loader.plan.spi.Return) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) FetchStyleLoadPlanBuildingAssociationVisitationStrategy(org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test)

Example 5 with Fetch

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

the class LoadPlanBuilderTest method testCascadeBasedBuild.

@Test
public void testCascadeBasedBuild() {
    EntityPersister ep = (EntityPersister) sessionFactory().getClassMetadata(Message.class);
    CascadeStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new CascadeStyleLoadPlanBuildingAssociationVisitationStrategy(CascadingActions.MERGE, sessionFactory(), LoadQueryInfluencers.NONE, LockMode.NONE);
    LoadPlan plan = MetamodelDrivenLoadPlanBuilder.buildRootEntityLoadPlan(strategy, ep);
    assertFalse(plan.hasAnyScalarReturns());
    assertEquals(1, plan.getReturns().size());
    Return rtn = plan.getReturns().get(0);
    EntityReturn entityReturn = ExtraAssertions.assertTyping(EntityReturn.class, rtn);
    assertNotNull(entityReturn.getFetches());
    assertEquals(1, entityReturn.getFetches().length);
    Fetch fetch = entityReturn.getFetches()[0];
    EntityFetch entityFetch = ExtraAssertions.assertTyping(EntityFetch.class, fetch);
    assertNotNull(entityFetch.getFetches());
    assertEquals(0, entityFetch.getFetches().length);
    LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sessionFactory()));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Fetch(org.hibernate.loader.plan.spi.Fetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) CascadeStyleLoadPlanBuildingAssociationVisitationStrategy(org.hibernate.loader.plan.build.internal.CascadeStyleLoadPlanBuildingAssociationVisitationStrategy) CollectionReturn(org.hibernate.loader.plan.spi.CollectionReturn) Return(org.hibernate.loader.plan.spi.Return) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test)

Aggregations

EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)5 Fetch (org.hibernate.loader.plan.spi.Fetch)5 AliasResolutionContextImpl (org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl)2 CollectionAttributeFetch (org.hibernate.loader.plan.spi.CollectionAttributeFetch)2 CollectionReturn (org.hibernate.loader.plan.spi.CollectionReturn)2 EntityReturn (org.hibernate.loader.plan.spi.EntityReturn)2 FetchSource (org.hibernate.loader.plan.spi.FetchSource)2 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)2 Return (org.hibernate.loader.plan.spi.Return)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Test (org.junit.Test)2 CascadeStyleLoadPlanBuildingAssociationVisitationStrategy (org.hibernate.loader.plan.build.internal.CascadeStyleLoadPlanBuildingAssociationVisitationStrategy)1 FetchStyleLoadPlanBuildingAssociationVisitationStrategy (org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy)1 EntityReferenceInitializerImpl (org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl)1 EntityReferenceInitializer (org.hibernate.loader.plan.exec.process.spi.EntityReferenceInitializer)1 EntityReferenceAliases (org.hibernate.loader.plan.exec.spi.EntityReferenceAliases)1 BidirectionalEntityReference (org.hibernate.loader.plan.spi.BidirectionalEntityReference)1 CompositeFetch (org.hibernate.loader.plan.spi.CompositeFetch)1 EntityReference (org.hibernate.loader.plan.spi.EntityReference)1 Joinable (org.hibernate.persister.entity.Joinable)1