Package org.hibernate.loader.plan.exec.spi

Examples of org.hibernate.loader.plan.exec.spi.CollectionReferenceAliases


            querySpace.getUid()
        )
    );

    final EntityReferenceAliases entityAliases = aliasResolutionContext.resolveEntityReferenceAliases( querySpace.getUid() );
    final CollectionReferenceAliases collectionReferenceAliases = aliasResolutionContext.resolveCollectionReferenceAliases( querySpace.getUid() );

    if ( entityAliases != null ) {
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
              + "alias suffix - " + entityAliases.getColumnAliases().getSuffix()
      );
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
              + "suffixed key columns - {"
              + StringHelper.join( ", ", entityAliases.getColumnAliases().getSuffixedKeyAliases() )
              + "}"
      );
    }

    if ( collectionReferenceAliases != null ) {
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
              + "alias suffix - " + collectionReferenceAliases.getCollectionColumnAliases().getSuffix()
      );
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
              + "suffixed key columns - {"
              + StringHelper.join( ", ", collectionReferenceAliases.getCollectionColumnAliases().getSuffixedKeyAliases() )
              + "}"
      );
      final EntityAliases elementAliases =
          collectionReferenceAliases.getEntityElementAliases() == null ?
              null :
              collectionReferenceAliases.getEntityElementAliases().getColumnAliases();
      if ( elementAliases != null ) {
        printWriter.println(
            TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
                + "entity-element alias suffix - " + elementAliases.getSuffix()
        );
View Full Code Here


    else {
      manyToManyTableAlias = null;
      tableAlias = createTableAlias( persister.getRole() );
    }

    final CollectionReferenceAliases collectionAliases = new CollectionReferenceAliasesImpl(
        tableAlias,
        manyToManyTableAlias,
        createCollectionAliases( persister ),
        createCollectionElementAliases( persister, tableAlias, elementQuerySpaceUid )
    );
View Full Code Here

        TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
            + "SQL table alias mapping - " + resolveSqlTableAliasFromQuerySpaceUid( querySpace.getUid() )
    );

    final EntityReferenceAliases entityAliases = resolveEntityReferenceAliases( querySpace.getUid() );
    final CollectionReferenceAliases collectionReferenceAliases = resolveCollectionReferenceAliases( querySpace.getUid() );

    if ( entityAliases != null ) {
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
              + "alias suffix - " + entityAliases.getColumnAliases().getSuffix()
      );
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
              + "suffixed key columns - "
              + StringHelper.join( ", ", entityAliases.getColumnAliases().getSuffixedKeyAliases() )
      );
    }

    if ( collectionReferenceAliases != null ) {
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
              + "alias suffix - " + collectionReferenceAliases.getCollectionColumnAliases().getSuffix()
      );
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
              + "suffixed key columns - "
              + StringHelper.join( ", ", collectionReferenceAliases.getCollectionColumnAliases().getSuffixedKeyAliases() )
      );
      final EntityReferenceAliases elementAliases = collectionReferenceAliases.getEntityElementAliases();
      if ( elementAliases != null ) {
        printWriter.println(
            TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
                + "entity-element alias suffix - " + elementAliases.getColumnAliases().getSuffix()
        );
View Full Code Here

      CollectionAttributeFetch fetch,
      ReaderCollector readerCollector,
      FetchStatsImpl fetchStats) {
    fetchStats.processingFetch( fetch );

    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( collectionTableAlias );
      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 );
      }
    }
View Full Code Here

      CollectionAttributeFetch fetch,
      ReaderCollector readerCollector,
      FetchStatsImpl fetchStats) {
    fetchStats.processingFetch( fetch );

    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 );
      }
    }
View Full Code Here

            querySpace.getUid()
        )
    );

    final EntityReferenceAliases entityAliases = aliasResolutionContext.resolveEntityReferenceAliases( querySpace.getUid() );
    final CollectionReferenceAliases collectionReferenceAliases = aliasResolutionContext.resolveCollectionReferenceAliases( querySpace.getUid() );

    if ( entityAliases != null ) {
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
              + "alias suffix - " + entityAliases.getColumnAliases().getSuffix()
      );
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
              + "suffixed key columns - {"
              + StringHelper.join( ", ", entityAliases.getColumnAliases().getSuffixedKeyAliases() )
              + "}"
      );
    }

    if ( collectionReferenceAliases != null ) {
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
              + "alias suffix - " + collectionReferenceAliases.getCollectionColumnAliases().getSuffix()
      );
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
              + "suffixed key columns - {"
              + StringHelper.join( ", ", collectionReferenceAliases.getCollectionColumnAliases().getSuffixedKeyAliases() )
              + "}"
      );
      final EntityAliases elementAliases = collectionReferenceAliases.getEntityElementColumnAliases();
      if ( elementAliases != null ) {
        printWriter.println(
            TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset )
                + "entity-element alias suffix - " + elementAliases.getSuffix()
        );
View Full Code Here

    );
  }

  private void renderCollectionJoin(Join join, JoinFragment joinFragment) {
    final CollectionQuerySpace rightHandSide = (CollectionQuerySpace) join.getRightHandSide();
    final CollectionReferenceAliases aliases = aliasResolutionContext.generateCollectionReferenceAliases(
        rightHandSide.getUid(),
        rightHandSide.getCollectionPersister()
    );

    // The SQL join to the "collection table" needs to be rendered.
    //
    // In the case of a basic collection, that's the only join needed.
    //
    // For one-to-many/many-to-many, we need to render the "collection table join"
    // here (as already stated). There will be a follow-on join (rhs will have a join) for the associated entity.
    // For many-to-many, the follow-on join will join to the associated entity element table. For one-to-many,
    // the collection table is the associated entity table, so the follow-on join will not be rendered..

    if ( rightHandSide.getCollectionPersister().isOneToMany()
        || rightHandSide.getCollectionPersister().isManyToMany() ) {
      // relatedly, for collections with entity elements (one-to-many, many-to-many) we need to register the
      // sql aliases to use for the entity.
      //
      // currently we do not explicitly track the joins under the CollectionQuerySpace to know which is
      // the element join and which is the index join (maybe we should?).  Another option here is to have the
      // "collection join" act as the entity element join in this case (much like I do with entity identifiers).
      // The difficulty there is that collections can theoretically could be multiple joins in that case (one
      // for element, one for index).  However, that's a bit of future-planning as today Hibernate does not
      // properly deal with the index anyway in terms of allowing dynamic fetching across a collection index...
      //
      // long story short, for now we'll use an assumption that the last join in the CollectionQuerySpace is the
      // element join (that's how the joins are built as of now..)
      //
      // todo : remove this assumption ^^; maybe we make CollectionQuerySpace "special" and rather than have it
      // hold a list of joins, we have it expose the 2 (index, element) separately.

      Join collectionElementJoin = null;
      for ( Join collectionJoin : rightHandSide.getJoins() ) {
        collectionElementJoin = collectionJoin;
      }
      if ( collectionElementJoin == null ) {
        throw new IllegalStateException(
            String.format(
                "Could not locate collection element join within collection join [%s : %s]",
                rightHandSide.getUid(),
                rightHandSide.getCollectionPersister()
            )
        );
      }
      aliasResolutionContext.registerQuerySpaceAliases(
          collectionElementJoin.getRightHandSide().getUid(),
          new EntityReferenceAliasesImpl(
              aliases.getElementTableAlias(),
              aliases.getEntityElementColumnAliases()
          )
      );
    }

    addJoins(
View Full Code Here

      CollectionAttributeFetch fetch,
      ReaderCollector readerCollector,
      FetchStatsImpl fetchStats) {
    fetchStats.processingFetch( fetch );

    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.getEntityElementColumnAliases().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.getEntityElementColumnAliases().getSuffix()
          )
      );

      // add SQL ORDER-BY fragments
      final String manyToManyOrdering = queryableCollection.getManyToManyOrderByString( collectionTableAlias );
      if ( StringHelper.isNotEmpty( manyToManyOrdering ) ) {
        selectStatementBuilder.appendOrderByFragment( manyToManyOrdering );
      }

      final String ordering = queryableCollection.getSQLOrderByString( collectionTableAlias );
      if ( StringHelper.isNotEmpty( ordering ) ) {
        selectStatementBuilder.appendOrderByFragment( ordering );
      }

      // add an EntityReferenceInitializer for the collection elements (keys also?)
      final EntityReferenceAliases entityReferenceAliases = new EntityReferenceAliasesImpl(
          aliases.getCollectionTableAlias(),
          aliases.getEntityElementColumnAliases()
      );
      aliasResolutionContext.registerQuerySpaceAliases( fetch.getQuerySpaceUid(), entityReferenceAliases );
      readerCollector.add(
          new EntityReferenceInitializerImpl(
              (EntityReference) fetch.getElementGraph(),
              entityReferenceAliases
          )
      );
    }
    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.getEntityElementColumnAliases().getSuffix()
            )
        );
        final EntityReferenceAliases entityReferenceAliases = new EntityReferenceAliasesImpl(
            aliases.getElementTableAlias(),
            aliases.getEntityElementColumnAliases()
        );
        aliasResolutionContext.registerQuerySpaceAliases( fetch.getQuerySpaceUid(), entityReferenceAliases );
        readerCollector.add(
            new EntityReferenceInitializerImpl(
                (EntityReference) fetch.getElementGraph(),
                entityReferenceAliases
            )
        );
      }

      final String ordering = queryableCollection.getSQLOrderByString( aliases.getElementTableAlias() );
      if ( StringHelper.isNotEmpty( ordering ) ) {
        selectStatementBuilder.appendOrderByFragment( ordering );
      }
    }
View Full Code Here

        TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
            + "SQL table alias mapping - " + resolveSqlTableAliasFromQuerySpaceUid( querySpace.getUid() )
    );

    final EntityReferenceAliases entityAliases = resolveEntityReferenceAliases( querySpace.getUid() );
    final CollectionReferenceAliases collectionReferenceAliases = resolveCollectionReferenceAliases( querySpace.getUid() );

    if ( entityAliases != null ) {
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
              + "alias suffix - " + entityAliases.getColumnAliases().getSuffix()
      );
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
              + "suffixed key columns - "
              + StringHelper.join( ", ", entityAliases.getColumnAliases().getSuffixedKeyAliases() )
      );
    }

    if ( collectionReferenceAliases != null ) {
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
              + "alias suffix - " + collectionReferenceAliases.getCollectionColumnAliases().getSuffix()
      );
      printWriter.println(
          TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
              + "suffixed key columns - "
              + StringHelper.join( ", ", collectionReferenceAliases.getCollectionColumnAliases().getSuffixedKeyAliases() )
      );
      final EntityAliases elementAliases = collectionReferenceAliases.getEntityElementColumnAliases();
      if ( elementAliases != null ) {
        printWriter.println(
            TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 )
                + "entity-element alias suffix - " + elementAliases.getSuffix()
        );
View Full Code Here

      AliasResolutionContext aliasResolutionContext,
      ReaderCollector readerCollector,
      FetchStatsImpl stats) {
    stats.processingFetch( fetch );

    final CollectionReferenceAliases aliases = aliasResolutionContext.resolveAliases( fetch );

    if ( fetch.getCollectionPersister().isManyToMany() ) {
      final QueryableCollection queryableCollection = (QueryableCollection) fetch.getCollectionPersister();
      final Joinable joinableCollection = (Joinable) fetch.getCollectionPersister();

      // 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 = resolveLhsTableAlias( fetchOwner, fetch, aliasResolutionContext );
      //    2) the m-n table : user_role
      final String collectionTableAlias = aliases.getCollectionTableAlias();
      //    3) the element table : role
      final String elementTableAlias = aliases.getElementTableAlias();

      {
        // add join fragments from the owner table -> collection table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        final String filterFragment = ( (Joinable) fetch.getCollectionPersister() ).filterFragment(
            collectionTableAlias,
            buildingParameters.getQueryInfluencers().getEnabledFilters()
        );

        joinFragment.addJoin(
            joinableCollection.getTableName(),
            collectionTableAlias,
            StringHelper.qualify( ownerTableAlias, extractJoinable( fetchOwner ).getKeyColumnNames() ),
            queryableCollection.getKeyColumnNames(),
            fetch.isNullable() ? JoinType.LEFT_OUTER_JOIN : JoinType.INNER_JOIN,
            filterFragment
        );
        joinFragment.addJoins(
            joinableCollection.fromJoinFragment( collectionTableAlias, false, true ),
            joinableCollection.whereJoinFragment( collectionTableAlias, false, true )
        );

        // add select fragments from the collection table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        selectStatementBuilder.appendSelectClauseFragment(
            joinableCollection.selectFragment(
                (Joinable) queryableCollection.getElementPersister(),
                ownerTableAlias,
                collectionTableAlias,
                aliases.getEntityElementColumnAliases().getSuffix(),
                aliases.getCollectionColumnAliases().getSuffix(),
                true
            )
        );
      }

      {
        // add join fragments from the collection table -> element entity table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        final String additionalJoinConditions = resolveAdditionalJoinCondition(
            factory,
            elementTableAlias,
            fetchOwner,
            fetch,
            buildingParameters.getQueryInfluencers(),
            aliasResolutionContext
        );

        final String manyToManyFilter = fetch.getCollectionPersister().getManyToManyFilterFragment(
            collectionTableAlias,
            buildingParameters.getQueryInfluencers().getEnabledFilters()
        );

        final String condition;
        if ( "".equals( manyToManyFilter ) ) {
          condition = additionalJoinConditions;
        }
        else if ( "".equals( additionalJoinConditions ) ) {
          condition = manyToManyFilter;
        }
        else {
          condition = additionalJoinConditions + " and " + manyToManyFilter;
        }

        final OuterJoinLoadable elementPersister = (OuterJoinLoadable) queryableCollection.getElementPersister();

        addJoins(
            joinFragment,
            elementPersister,
//            JoinType.INNER_JOIN,
            JoinType.LEFT_OUTER_JOIN,
            elementTableAlias,
            elementPersister.getIdentifierColumnNames(),
            StringHelper.qualify( collectionTableAlias, queryableCollection.getElementColumnNames() ),
            condition
        );

        // add select fragments from the element entity table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        selectStatementBuilder.appendSelectClauseFragment(
            elementPersister.selectFragment(
                aliases.getElementTableAlias(),
                aliases.getEntityElementColumnAliases().getSuffix()
            )
        );
      }

      final String manyToManyOrdering = queryableCollection.getManyToManyOrderByString( collectionTableAlias );
      if ( StringHelper.isNotEmpty( manyToManyOrdering ) ) {
        selectStatementBuilder.appendOrderByFragment( manyToManyOrdering );
      }

      final String ordering = queryableCollection.getSQLOrderByString( collectionTableAlias );
      if ( StringHelper.isNotEmpty( ordering ) ) {
        selectStatementBuilder.appendOrderByFragment( ordering );
      }


      final EntityReferenceAliases entityReferenceAliases = new EntityReferenceAliases() {
        @Override
        public String getTableAlias() {
          return aliases.getElementTableAlias();
        }

        @Override
        public EntityAliases getColumnAliases() {
          return aliases.getEntityElementColumnAliases();
        }
      };

      final EntityReference elementEntityReference = (EntityReference) fetch.getElementGraph();
      readerCollector.addReader(
          new EntityReferenceReader(
              elementEntityReference,
              entityReferenceAliases,
              buildIdentifierReader(
                  selectStatementBuilder,
                  factory,
                  joinFragment,
                  elementEntityReference,
                  buildingParameters,
                  aliasResolutionContext,
                  readerCollector,
                  entityReferenceAliases,
                  stats
              )
          )
      );
    }
    else {
      final QueryableCollection queryableCollection = (QueryableCollection) fetch.getCollectionPersister();
      final Joinable joinableCollection = (Joinable) fetch.getCollectionPersister();

      final String rhsTableAlias = aliases.getElementTableAlias();
      final String[] rhsColumnNames = JoinHelper.getRHSColumnNames( fetch.getFetchedType(), factory );

      final String lhsTableAlias = resolveLhsTableAlias( fetchOwner, fetch, aliasResolutionContext );
      // todo : this is not exactly correct.  it assumes the join refers to the LHS PK
      final String[] aliasedLhsColumnNames = fetch.toSqlSelectFragments( lhsTableAlias );

      final String on = resolveAdditionalJoinCondition(
          factory,
          rhsTableAlias,
          fetchOwner,
          fetch,
          buildingParameters.getQueryInfluencers(),
          aliasResolutionContext
      );

      addJoins(
          joinFragment,
          joinableCollection,
          fetch.isNullable() ? JoinType.LEFT_OUTER_JOIN : JoinType.INNER_JOIN,
          rhsTableAlias,
          rhsColumnNames,
          aliasedLhsColumnNames,
          on
      );

      // select the "collection columns"
      selectStatementBuilder.appendSelectClauseFragment(
          queryableCollection.selectFragment(
              rhsTableAlias,
              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.getEntityElementColumnAliases().getSuffix()
            )
        );

        final EntityReferenceAliases entityReferenceAliases = new EntityReferenceAliases() {
          @Override
          public String getTableAlias() {
            return aliases.getElementTableAlias();
          }

          @Override
          public EntityAliases getColumnAliases() {
            return aliases.getEntityElementColumnAliases();
          }
        };

        final EntityReference elementEntityReference = (EntityReference) fetch.getElementGraph();
        readerCollector.addReader(
View Full Code Here

TOP

Related Classes of org.hibernate.loader.plan.exec.spi.CollectionReferenceAliases

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.