Search in sources :

Example 11 with OuterJoinLoadable

use of org.hibernate.persister.entity.OuterJoinLoadable in project hibernate-orm by hibernate.

the class LoadQueryJoinAndFetchProcessor method processCollectionFetch.

private void processCollectionFetch(SelectStatementBuilder selectStatementBuilder, FetchSource fetchSource, CollectionAttributeFetch fetch, ReaderCollector readerCollector, FetchStatsImpl fetchStats) {
    fetchStats.processingFetch(fetch);
    if (!FetchStrategyHelper.isJoinFetched(fetch.getFetchStrategy())) {
        // not join fetched, so nothing else to do
        return;
    }
    final CollectionReferenceAliases aliases = aliasResolutionContext.resolveCollectionReferenceAliases(fetch.getQuerySpaceUid());
    final QueryableCollection queryableCollection = (QueryableCollection) fetch.getCollectionPersister();
    final Joinable joinableCollection = (Joinable) fetch.getCollectionPersister();
    if (fetch.getCollectionPersister().isManyToMany()) {
        // todo : better way to access `ownerTableAlias` here.
        // when processing the Join part of this we are able to look up the "lhs table alias" because we know
        // the 'lhs' QuerySpace.
        // 
        // Good idea to be able resolve a Join by lookup on the rhs and lhs uid?  If so, Fetch
        // for many-to-many we have 3 table aliases.  By way of example, consider a normal m-n: User<->Role
        // where User is the FetchOwner and Role (User.roles) is the Fetch.  We'd have:
        // 1) the owner's table : user
        final String ownerTableAlias = aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(fetchSource.getQuerySpaceUid());
        // 2) the m-n table : user_role
        final String collectionTableAlias = aliases.getCollectionTableAlias();
        // 3) the element table : role
        final String elementTableAlias = aliases.getElementTableAlias();
        // add select fragments from the collection table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        selectStatementBuilder.appendSelectClauseFragment(joinableCollection.selectFragment((Joinable) queryableCollection.getElementPersister(), elementTableAlias, collectionTableAlias, aliases.getEntityElementAliases().getColumnAliases().getSuffix(), aliases.getCollectionColumnAliases().getSuffix(), true));
        // add select fragments from the element entity table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        final OuterJoinLoadable elementPersister = (OuterJoinLoadable) queryableCollection.getElementPersister();
        selectStatementBuilder.appendSelectClauseFragment(elementPersister.selectFragment(elementTableAlias, aliases.getEntityElementAliases().getColumnAliases().getSuffix()));
        // add SQL ORDER-BY fragments
        final String manyToManyOrdering = queryableCollection.getManyToManyOrderByString(elementTableAlias);
        if (StringHelper.isNotEmpty(manyToManyOrdering)) {
            selectStatementBuilder.appendOrderByFragment(manyToManyOrdering);
        }
        final String ordering = queryableCollection.getSQLOrderByString(collectionTableAlias);
        if (StringHelper.isNotEmpty(ordering)) {
            selectStatementBuilder.appendOrderByFragment(ordering);
        }
        readerCollector.add(new EntityReferenceInitializerImpl((EntityReference) fetch.getElementGraph(), aliasResolutionContext.resolveEntityReferenceAliases(fetch.getElementGraph().getQuerySpaceUid())));
    } else {
        // select the "collection columns"
        selectStatementBuilder.appendSelectClauseFragment(queryableCollection.selectFragment(aliases.getElementTableAlias(), aliases.getCollectionColumnAliases().getSuffix()));
        if (fetch.getCollectionPersister().isOneToMany()) {
            // if the collection elements are entities, select the entity columns as well
            final OuterJoinLoadable elementPersister = (OuterJoinLoadable) queryableCollection.getElementPersister();
            selectStatementBuilder.appendSelectClauseFragment(elementPersister.selectFragment(aliases.getElementTableAlias(), aliases.getEntityElementAliases().getColumnAliases().getSuffix()));
            readerCollector.add(new EntityReferenceInitializerImpl((EntityReference) fetch.getElementGraph(), aliasResolutionContext.resolveEntityReferenceAliases(fetch.getElementGraph().getQuerySpaceUid())));
        }
        final String ordering = queryableCollection.getSQLOrderByString(aliases.getElementTableAlias());
        if (StringHelper.isNotEmpty(ordering)) {
            selectStatementBuilder.appendOrderByFragment(ordering);
        }
    }
    if (fetch.getElementGraph() != null) {
        processFetches(fetch.getElementGraph(), selectStatementBuilder, readerCollector);
    }
    readerCollector.add(new CollectionReferenceInitializerImpl(fetch, aliases));
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) Joinable(org.hibernate.persister.entity.Joinable) EntityReferenceInitializerImpl(org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl) EntityReference(org.hibernate.loader.plan.spi.EntityReference) CollectionReferenceAliases(org.hibernate.loader.plan.exec.spi.CollectionReferenceAliases) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) CollectionReferenceInitializerImpl(org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl)

Example 12 with OuterJoinLoadable

use of org.hibernate.persister.entity.OuterJoinLoadable in project hibernate-orm by hibernate.

the class OneToManyLoadQueryDetails method applyRootReturnTableFragments.

@Override
protected void applyRootReturnTableFragments(SelectStatementBuilder selectStatementBuilder) {
    final OuterJoinLoadable elementOuterJoinLoadable = (OuterJoinLoadable) getElementEntityReference().getEntityPersister();
    // final String tableAlias = getCollectionReferenceAliases().getCollectionTableAlias();
    final String tableAlias = getElementEntityReferenceAliases().getTableAlias();
    final String fragment = elementOuterJoinLoadable.fromTableFragment(tableAlias) + elementOuterJoinLoadable.fromJoinFragment(tableAlias, true, true);
    selectStatementBuilder.appendFromClauseFragment(fragment);
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable)

Example 13 with OuterJoinLoadable

use of org.hibernate.persister.entity.OuterJoinLoadable in project hibernate-orm by hibernate.

the class EntityBasedAssociationAttribute method getAssociationKey.

@Override
public AssociationKey getAssociationKey() {
    final AssociationType type = getType();
    if (type.isAnyType()) {
        return new AssociationKey(JoinHelper.getLHSTableName(type, attributeNumber(), (OuterJoinLoadable) getSource()), JoinHelper.getLHSColumnNames(type, attributeNumber(), 0, (OuterJoinLoadable) getSource(), sessionFactory()));
    }
    final Joinable joinable = type.getAssociatedJoinable(sessionFactory());
    if (type.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
        final String lhsTableName;
        final String[] lhsColumnNames;
        if (joinable.isCollection()) {
            final QueryableCollection collectionPersister = (QueryableCollection) joinable;
            lhsTableName = collectionPersister.getTableName();
            lhsColumnNames = collectionPersister.getElementColumnNames();
        } else {
            final OuterJoinLoadable entityPersister = (OuterJoinLoadable) source();
            lhsTableName = getLHSTableName(type, attributeNumber(), entityPersister);
            lhsColumnNames = getLHSColumnNames(type, attributeNumber(), entityPersister, sessionFactory());
        }
        return new AssociationKey(lhsTableName, lhsColumnNames);
    } else {
        return new AssociationKey(joinable.getTableName(), getRHSColumnNames(type, sessionFactory()));
    }
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 14 with OuterJoinLoadable

use of org.hibernate.persister.entity.OuterJoinLoadable in project hibernate-orm by hibernate.

the class EntityBasedAssociationAttribute method determineFetchPlan.

@Override
public FetchStrategy determineFetchPlan(LoadQueryInfluencers loadQueryInfluencers, PropertyPath propertyPath) {
    final EntityPersister owningPersister = getSource().getEntityPersister();
    FetchStyle style = FetchStrategyHelper.determineFetchStyleByProfile(loadQueryInfluencers, owningPersister, propertyPath, attributeNumber());
    if (style == null) {
        style = FetchStrategyHelper.determineFetchStyleByMetadata(((OuterJoinLoadable) getSource().getEntityPersister()).getFetchMode(attributeNumber()), getType(), sessionFactory());
    }
    return new FetchStrategy(FetchStrategyHelper.determineFetchTiming(style, getType(), sessionFactory()), style);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) FetchStyle(org.hibernate.engine.FetchStyle) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) FetchStrategy(org.hibernate.engine.FetchStrategy)

Example 15 with OuterJoinLoadable

use of org.hibernate.persister.entity.OuterJoinLoadable in project hibernate-orm by hibernate.

the class NoProxyFetchStrategyHelperTest method determineFetchMode.

private org.hibernate.FetchMode determineFetchMode(Class<?> entityClass, String path) {
    OuterJoinLoadable entityPersister = (OuterJoinLoadable) sessionFactory().getEntityPersister(entityClass.getName());
    int index = ((UniqueKeyLoadable) entityPersister).getPropertyIndex(path);
    return entityPersister.getFetchMode(index);
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable)

Aggregations

OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)22 Joinable (org.hibernate.persister.entity.Joinable)6 UniqueKeyLoadable (org.hibernate.persister.entity.UniqueKeyLoadable)6 AssociationType (org.hibernate.type.AssociationType)5 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 Configuration (org.hibernate.cfg.Configuration)2 EntityReturn (org.hibernate.loader.plan.spi.EntityReturn)2 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)2 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)2 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)2 Test (org.junit.Test)2 Iterator (java.util.Iterator)1 Dialect (org.hibernate.dialect.Dialect)1 FetchStrategy (org.hibernate.engine.FetchStrategy)1 FetchStyle (org.hibernate.engine.FetchStyle)1 Fetch (org.hibernate.engine.profile.Fetch)1 FetchProfile (org.hibernate.engine.profile.FetchProfile)1 CriteriaJoinWalker (org.hibernate.loader.criteria.CriteriaJoinWalker)1 CollectionFetchableElementEntityGraph (org.hibernate.loader.plan.build.internal.returns.CollectionFetchableElementEntityGraph)1