Search in sources :

Example 6 with EntityReference

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

the class AbstractRowReader method resolveEntityKey.

private void resolveEntityKey(ResultSet resultSet, ResultSetProcessingContextImpl context, EntityReferenceInitializer entityReferenceInitializer) throws SQLException {
    final EntityReference entityReference = entityReferenceInitializer.getEntityReference();
    final EntityIdentifierDescription identifierDescription = entityReference.getIdentifierDescription();
    if (identifierDescription.hasFetches() || identifierDescription.hasBidirectionalEntityReferences()) {
        resolveEntityKey(resultSet, context, (FetchSource) identifierDescription);
    }
    entityReferenceInitializer.resolveEntityKey(resultSet, context);
}
Also used : EntityReference(org.hibernate.loader.plan.spi.EntityReference) BidirectionalEntityReference(org.hibernate.loader.plan.spi.BidirectionalEntityReference) EntityIdentifierDescription(org.hibernate.loader.plan.spi.EntityIdentifierDescription)

Example 7 with EntityReference

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

the class ResultSetProcessingContextImpl method createSubselects.

private void createSubselects() {
    if (subselectLoadableEntityKeyMap == null || nRowsRead <= 1) {
        LOG.tracef("Skipping create subselects because there are fewer than 2 results, so query by key is more efficient.", getClass().getName());
        // early return
        return;
    }
    final Map<String, int[]> namedParameterLocMap = ResultSetProcessorHelper.buildNamedParameterLocMap(queryParameters, namedParameterContext);
    final String subselectQueryString = SubselectFetch.createSubselectFetchQueryFragment(queryParameters);
    for (Map.Entry<EntityReference, Set<EntityKey>> entry : subselectLoadableEntityKeyMap.entrySet()) {
        if (!entry.getKey().getEntityPersister().hasSubselectLoadableCollections()) {
            continue;
        }
        SubselectFetch subselectFetch = new SubselectFetch(subselectQueryString, aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(entry.getKey().getQuerySpaceUid()), (Loadable) entry.getKey().getEntityPersister(), queryParameters, entry.getValue(), namedParameterLocMap);
        for (EntityKey key : entry.getValue()) {
            session.getPersistenceContext().getBatchFetchQueue().addSubselect(key, subselectFetch);
        }
    }
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) EntityReference(org.hibernate.loader.plan.spi.EntityReference) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 8 with EntityReference

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

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

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method startingEntityIdentifier.

// entity identifiers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public void startingEntityIdentifier(EntityIdentifierDefinition entityIdentifierDefinition) {
    log.tracef("%s Starting entity identifier : %s", StringHelper.repeat(">>", fetchSourceStack.size()), entityIdentifierDefinition.getEntityDefinition().getEntityPersister().getEntityName());
    final EntityReference entityReference = (EntityReference) currentSource();
    // perform some stack validation
    if (!entityReference.getEntityPersister().equals(entityIdentifierDefinition.getEntityDefinition().getEntityPersister())) {
        throw new WalkingException(String.format("Encountered unexpected fetch owner [%s] in stack while processing entity identifier for [%s]", entityReference.getEntityPersister().getEntityName(), entityIdentifierDefinition.getEntityDefinition().getEntityPersister().getEntityName()));
    }
    if (ExpandingEntityIdentifierDescription.class.isInstance(entityReference.getIdentifierDescription())) {
        pushToStack((ExpandingEntityIdentifierDescription) entityReference.getIdentifierDescription());
    }
}
Also used : EntityReference(org.hibernate.loader.plan.spi.EntityReference) WalkingException(org.hibernate.persister.walking.spi.WalkingException)

Example 10 with EntityReference

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

the class LoadPlanBuilderTest method testCollectionInitializerCase.

@Test
public void testCollectionInitializerCase() {
    CollectionPersister cp = sessionFactory().getCollectionPersister(Poster.class.getName() + ".messages");
    FetchStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new FetchStyleLoadPlanBuildingAssociationVisitationStrategy(sessionFactory(), LoadQueryInfluencers.NONE, LockMode.NONE);
    LoadPlan plan = MetamodelDrivenLoadPlanBuilder.buildRootCollectionLoadPlan(strategy, cp);
    assertFalse(plan.hasAnyScalarReturns());
    assertEquals(1, plan.getReturns().size());
    Return rtn = plan.getReturns().get(0);
    CollectionReturn collectionReturn = ExtraAssertions.assertTyping(CollectionReturn.class, rtn);
    assertNotNull(collectionReturn.getElementGraph());
    assertNotNull(collectionReturn.getElementGraph().getFetches());
    // the collection Message elements are fetched, but Message.poster is not fetched
    // (because that collection is owned by that Poster)
    assertEquals(0, collectionReturn.getElementGraph().getFetches().length);
    EntityReference entityReference = ExtraAssertions.assertTyping(EntityReference.class, collectionReturn.getElementGraph());
    assertNotNull(entityReference.getFetches());
    assertEquals(0, entityReference.getFetches().length);
    LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sessionFactory()));
}
Also used : AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) 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) CollectionReturn(org.hibernate.loader.plan.spi.CollectionReturn) FetchStyleLoadPlanBuildingAssociationVisitationStrategy(org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy) EntityReference(org.hibernate.loader.plan.spi.EntityReference) Test(org.junit.Test)

Aggregations

EntityReference (org.hibernate.loader.plan.spi.EntityReference)10 EntityKey (org.hibernate.engine.spi.EntityKey)2 BidirectionalEntityReference (org.hibernate.loader.plan.spi.BidirectionalEntityReference)2 EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)2 WalkingException (org.hibernate.persister.walking.spi.WalkingException)2 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)1 FetchStyleLoadPlanBuildingAssociationVisitationStrategy (org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy)1 ExpandingEntityIdentifierDescription (org.hibernate.loader.plan.build.spi.ExpandingEntityIdentifierDescription)1 ExpandingFetchSource (org.hibernate.loader.plan.build.spi.ExpandingFetchSource)1 AliasResolutionContextImpl (org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl)1 CollectionReferenceInitializerImpl (org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl)1 EntityReferenceInitializerImpl (org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl)1 CollectionReferenceAliases (org.hibernate.loader.plan.exec.spi.CollectionReferenceAliases)1 EntityReferenceAliases (org.hibernate.loader.plan.exec.spi.EntityReferenceAliases)1