Search in sources :

Example 6 with EntityFetch

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

the class EntityReferenceInitializerImpl method loadFromResultSet.

private void loadFromResultSet(ResultSet resultSet, ResultSetProcessingContext context, Object entityInstance, String concreteEntityTypeName, EntityKey entityKey, LockMode lockModeToAcquire) {
    final Serializable id = entityKey.getIdentifier();
    // Get the persister for the _subclass_
    final Loadable concreteEntityPersister = (Loadable) context.getSession().getFactory().getMetamodel().entityPersister(concreteEntityTypeName);
    if (log.isTraceEnabled()) {
        log.tracev("Initializing object from ResultSet: {0}", MessageHelper.infoString(concreteEntityPersister, id, context.getSession().getFactory()));
    }
    // add temp entry so that the next step is circular-reference
    // safe - only needed because some types don't take proper
    // advantage of two-phase-load (esp. components)
    TwoPhaseLoad.addUninitializedEntity(entityKey, entityInstance, concreteEntityPersister, lockModeToAcquire, context.getSession());
    final EntityPersister rootEntityPersister = context.getSession().getFactory().getMetamodel().entityPersister(concreteEntityPersister.getRootEntityName());
    final Object[] values;
    try {
        values = concreteEntityPersister.hydrate(resultSet, id, entityInstance, (Loadable) entityReference.getEntityPersister(), concreteEntityPersister == rootEntityPersister ? entityReferenceAliases.getColumnAliases().getSuffixedPropertyAliases() : entityReferenceAliases.getColumnAliases().getSuffixedPropertyAliases(concreteEntityPersister), context.getLoadPlan().areLazyAttributesForceFetched(), context.getSession());
        context.getProcessingState(entityReference).registerHydratedState(values);
    } catch (SQLException e) {
        throw context.getSession().getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlExceptionHelper().convert(e, "Could not read entity state from ResultSet : " + entityKey);
    }
    final Object rowId;
    try {
        rowId = concreteEntityPersister.hasRowId() ? resultSet.getObject(entityReferenceAliases.getColumnAliases().getRowIdAlias()) : null;
    } catch (SQLException e) {
        throw context.getSession().getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlExceptionHelper().convert(e, "Could not read entity row-id from ResultSet : " + entityKey);
    }
    final EntityType entityType = EntityFetch.class.isInstance(entityReference) ? ((EntityFetch) entityReference).getFetchedType() : entityReference.getEntityPersister().getEntityMetamodel().getEntityType();
    if (entityType != null) {
        String ukName = entityType.getRHSUniqueKeyPropertyName();
        if (ukName != null) {
            final int index = ((UniqueKeyLoadable) concreteEntityPersister).getPropertyIndex(ukName);
            final Type type = concreteEntityPersister.getPropertyTypes()[index];
            // polymorphism not really handled completely correctly,
            // perhaps...well, actually its ok, assuming that the
            // entity name used in the lookup is the same as the
            // the one used here, which it will be
            EntityUniqueKey euk = new EntityUniqueKey(entityReference.getEntityPersister().getEntityName(), ukName, type.semiResolve(values[index], context.getSession(), entityInstance), type, concreteEntityPersister.getEntityMode(), context.getSession().getFactory());
            context.getSession().getPersistenceContext().addEntity(euk, entityInstance);
        }
    }
    TwoPhaseLoad.postHydrate(concreteEntityPersister, id, values, rowId, entityInstance, lockModeToAcquire, context.getSession());
    context.registerHydratedEntity(entityReference, entityKey, entityInstance);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) Loadable(org.hibernate.persister.entity.Loadable) Serializable(java.io.Serializable) SQLException(java.sql.SQLException) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) EntityType(org.hibernate.type.EntityType) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityType(org.hibernate.type.EntityType) VersionType(org.hibernate.type.VersionType) Type(org.hibernate.type.Type) EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey)

Example 7 with EntityFetch

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

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method handleAssociationAttribute.

protected boolean handleAssociationAttribute(AssociationAttributeDefinition attributeDefinition) {
    // todo : this seems to not be correct for one-to-one
    final FetchStrategy fetchStrategy = determineFetchStrategy(attributeDefinition);
    final ExpandingFetchSource currentSource = currentSource();
    currentSource.validateFetchPlan(fetchStrategy, attributeDefinition);
    final AssociationAttributeDefinition.AssociationNature nature = attributeDefinition.getAssociationNature();
    if (nature == AssociationAttributeDefinition.AssociationNature.ANY) {
        // for ANY mappings we need to build a Fetch:
        //		1) fetch type is SELECT
        //		2) (because the fetch cannot be a JOIN...) do not push it to the stack
        // regardless of the fetch style, build the fetch
        currentSource.buildAnyAttributeFetch(attributeDefinition, fetchStrategy);
        return false;
    } else if (nature == AssociationAttributeDefinition.AssociationNature.ENTITY) {
        // regardless of the fetch style, build the fetch
        EntityFetch fetch = currentSource.buildEntityAttributeFetch(attributeDefinition, fetchStrategy);
        if (FetchStrategyHelper.isJoinFetched(fetchStrategy)) {
            // only push to the stack if join fetched
            pushToStack((ExpandingFetchSource) fetch);
            return true;
        } else {
            return false;
        }
    } else {
        // Collection
        // regardless of the fetch style, build the fetch
        CollectionAttributeFetch fetch = currentSource.buildCollectionAttributeFetch(attributeDefinition, fetchStrategy);
        if (FetchStrategyHelper.isJoinFetched(fetchStrategy)) {
            // only push to the stack if join fetched
            pushToCollectionStack(fetch);
            return true;
        } else {
            return false;
        }
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) FetchStrategy(org.hibernate.engine.FetchStrategy) ExpandingFetchSource(org.hibernate.loader.plan.build.spi.ExpandingFetchSource) AssociationAttributeDefinition(org.hibernate.persister.walking.spi.AssociationAttributeDefinition)

Example 8 with EntityFetch

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

the class AbstractExpandingFetchSource method buildEntityAttributeFetch.

@Override
public EntityFetch buildEntityAttributeFetch(AssociationAttributeDefinition attributeDefinition, FetchStrategy fetchStrategy) {
    final ExpandingEntityQuerySpace entityQuerySpace = QuerySpaceHelper.INSTANCE.makeEntityQuerySpace(expandingQuerySpace(), attributeDefinition, getQuerySpaces().generateImplicitUid(), fetchStrategy);
    final EntityFetch fetch = new EntityAttributeFetchImpl(this, attributeDefinition, fetchStrategy, entityQuerySpace);
    addFetch(fetch);
    return fetch;
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) ExpandingEntityQuerySpace(org.hibernate.loader.plan.build.spi.ExpandingEntityQuerySpace)

Example 9 with EntityFetch

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

the class ReturnGraphTreePrinter method writeFetch.

private void writeFetch(Fetch fetch, int depth, PrintWriter printWriter) {
    printWriter.print(TreePrinterHelper.INSTANCE.generateNodePrefix(depth));
    if (EntityFetch.class.isInstance(fetch)) {
        final EntityFetch entityFetch = (EntityFetch) fetch;
        printWriter.println(extractDetails(entityFetch));
        writeEntityReferenceFetches(entityFetch, depth + 1, printWriter);
    } else if (CompositeFetch.class.isInstance(fetch)) {
        final CompositeFetch compositeFetch = (CompositeFetch) fetch;
        printWriter.println(extractDetails(compositeFetch));
        writeCompositeFetchFetches(compositeFetch, depth + 1, printWriter);
    } else if (CollectionAttributeFetch.class.isInstance(fetch)) {
        final CollectionAttributeFetch collectionFetch = (CollectionAttributeFetch) fetch;
        printWriter.println(extractDetails(collectionFetch));
        writeCollectionReferenceFetches(collectionFetch, depth + 1, printWriter);
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) CompositeFetch(org.hibernate.loader.plan.spi.CompositeFetch)

Example 10 with EntityFetch

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

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