Search in sources :

Example 1 with EntityReturn

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

the class ReturnGraphTreePrinter method write.

public void write(Return rootReturn, int depth, PrintWriter printWriter) {
    if (rootReturn == null) {
        printWriter.println("Return is null!");
        return;
    }
    printWriter.write(TreePrinterHelper.INSTANCE.generateNodePrefix(depth));
    if (ScalarReturn.class.isInstance(rootReturn)) {
        printWriter.println(extractDetails((ScalarReturn) rootReturn));
    } else if (EntityReturn.class.isInstance(rootReturn)) {
        final EntityReturn entityReturn = (EntityReturn) rootReturn;
        printWriter.println(extractDetails(entityReturn));
        writeEntityReferenceFetches(entityReturn, depth + 1, printWriter);
    } else if (CollectionReference.class.isInstance(rootReturn)) {
        final CollectionReference collectionReference = (CollectionReference) rootReturn;
        printWriter.println(extractDetails(collectionReference));
        writeCollectionReferenceFetches(collectionReference, depth + 1, printWriter);
    }
    printWriter.flush();
}
Also used : ScalarReturn(org.hibernate.loader.plan.spi.ScalarReturn) CollectionReference(org.hibernate.loader.plan.spi.CollectionReference) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn)

Example 2 with EntityReturn

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

the class BatchingLoadQueryDetailsFactory method makeEntityLoadQueryDetails.

/**
 * Returns a EntityLoadQueryDetails object from the given inputs.
 *
 * @param loadPlan The load plan
 * @param keyColumnNames The columns to load the entity by (the PK columns or some other unique set of columns)
 * @param buildingParameters And influencers that would affect the generated SQL (mostly we are concerned with those
 * that add additional joins here)
 * @param factory The SessionFactory
 *
 * @return The EntityLoadQueryDetails
 */
public LoadQueryDetails makeEntityLoadQueryDetails(LoadPlan loadPlan, String[] keyColumnNames, QueryBuildingParameters buildingParameters, SessionFactoryImplementor factory) {
    // TODO: how should shouldUseOptionalEntityInformation be used?
    // final int batchSize = buildingParameters.getBatchSize();
    // final boolean shouldUseOptionalEntityInformation = batchSize == 1;
    final EntityReturn rootReturn = RootHelper.INSTANCE.extractRootReturn(loadPlan, EntityReturn.class);
    final String[] keyColumnNamesToUse = keyColumnNames != null ? keyColumnNames : ((Queryable) rootReturn.getEntityPersister()).getIdentifierColumnNames();
    // Should be just one querySpace (of type EntityQuerySpace) in querySpaces.  Should we validate that?
    // Should we make it a util method on Helper like we do for extractRootReturn ?
    final AliasResolutionContextImpl aliasResolutionContext = new AliasResolutionContextImpl(factory);
    return new EntityLoadQueryDetails(loadPlan, keyColumnNamesToUse, aliasResolutionContext, rootReturn, buildingParameters, factory);
}
Also used : EntityReturn(org.hibernate.loader.plan.spi.EntityReturn)

Example 3 with EntityReturn

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

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

Example 5 with EntityReturn

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

the class LoadPlanStructureAssertionTest method testEncapsulatedCompositeIdWithFetches2.

@Test
public void testEncapsulatedCompositeIdWithFetches2() {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Card.class);
    cfg.addAnnotatedClass(CardField.class);
    cfg.addAnnotatedClass(Key.class);
    cfg.addAnnotatedClass(PrimaryKey.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
    try {
        final OuterJoinLoadable cardPersister = (OuterJoinLoadable) sf.getClassMetadata(Card.class);
        doCompare(sf, cardPersister);
        final LoadPlan cardLoadPlan = LoadPlanStructureAssertionHelper.INSTANCE.buildLoadPlan(sf, cardPersister);
        assertEquals(LoadPlan.Disposition.ENTITY_LOADER, cardLoadPlan.getDisposition());
        assertEquals(1, cardLoadPlan.getReturns().size());
        // Check the root EntityReturn(Card)
        final EntityReturn cardReturn = assertTyping(EntityReturn.class, cardLoadPlan.getReturns().get(0));
        assertFalse(cardReturn.getIdentifierDescription().hasFetches());
        // Card should have one fetch, the fields collection
        assertEquals(1, cardReturn.getFetches().length);
        final CollectionAttributeFetch fieldsFetch = assertTyping(CollectionAttributeFetch.class, cardReturn.getFetches()[0]);
        assertNotNull(fieldsFetch.getElementGraph());
        // the Card.fields collection has entity elements of type CardField...
        final CollectionFetchableElementEntityGraph cardFieldElementGraph = assertTyping(CollectionFetchableElementEntityGraph.class, fieldsFetch.getElementGraph());
        // CardField should have no fetches
        assertEquals(0, cardFieldElementGraph.getFetches().length);
        // But it should have 1 key-many-to-one fetch for Key (Card is already handled)
        assertTrue(cardFieldElementGraph.getIdentifierDescription().hasFetches());
        final FetchSource cardFieldElementGraphIdAsFetchSource = assertTyping(FetchSource.class, cardFieldElementGraph.getIdentifierDescription());
        assertEquals(1, cardFieldElementGraphIdAsFetchSource.getFetches().length);
        assertEquals(1, cardFieldElementGraphIdAsFetchSource.getBidirectionalEntityReferences().length);
        BidirectionalEntityReference circularCardFetch = assertTyping(BidirectionalEntityReference.class, cardFieldElementGraphIdAsFetchSource.getBidirectionalEntityReferences()[0]);
        assertSame(circularCardFetch.getTargetEntityReference(), cardReturn);
        // the fetch above is to the other key-many-to-one for CardField.primaryKey composite: key
        EntityFetch keyFetch = assertTyping(EntityFetch.class, cardFieldElementGraphIdAsFetchSource.getFetches()[0]);
        assertEquals(Key.class.getName(), keyFetch.getEntityPersister().getEntityName());
    } finally {
        sf.close();
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) CollectionFetchableElementEntityGraph(org.hibernate.loader.plan.build.internal.returns.CollectionFetchableElementEntityGraph) FetchSource(org.hibernate.loader.plan.spi.FetchSource) Configuration(org.hibernate.cfg.Configuration) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) BidirectionalEntityReference(org.hibernate.loader.plan.spi.BidirectionalEntityReference) Key(org.hibernate.test.annotations.cid.keymanytoone.Key) PrimaryKey(org.hibernate.test.annotations.cid.keymanytoone.PrimaryKey) Card(org.hibernate.test.annotations.cid.keymanytoone.Card) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test) EncapsulatedCompositeIdResultSetProcessorTest(org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)

Aggregations

EntityReturn (org.hibernate.loader.plan.spi.EntityReturn)6 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)4 Test (org.junit.Test)4 EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)3 Configuration (org.hibernate.cfg.Configuration)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 AliasResolutionContextImpl (org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl)2 CollectionReturn (org.hibernate.loader.plan.spi.CollectionReturn)2 Fetch (org.hibernate.loader.plan.spi.Fetch)2 Return (org.hibernate.loader.plan.spi.Return)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)2 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)2 CascadeStyleLoadPlanBuildingAssociationVisitationStrategy (org.hibernate.loader.plan.build.internal.CascadeStyleLoadPlanBuildingAssociationVisitationStrategy)1 FetchStyleLoadPlanBuildingAssociationVisitationStrategy (org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy)1 CollectionFetchableElementEntityGraph (org.hibernate.loader.plan.build.internal.returns.CollectionFetchableElementEntityGraph)1 BidirectionalEntityReference (org.hibernate.loader.plan.spi.BidirectionalEntityReference)1 CollectionAttributeFetch (org.hibernate.loader.plan.spi.CollectionAttributeFetch)1 CollectionReference (org.hibernate.loader.plan.spi.CollectionReference)1 FetchSource (org.hibernate.loader.plan.spi.FetchSource)1