Package org.hibernate.loader.plan2.exec.process.internal

Examples of org.hibernate.loader.plan2.exec.process.internal.EntityReferenceInitializerImpl


    return entityReferenceProcessingState;
  }

  @Override
  public Object read(ResultSet resultSet, ResultSetProcessingContext context) throws SQLException {
    final EntityReferenceProcessingState processingState = getIdentifierResolutionContext( context );

    final EntityKey entityKey = processingState.getEntityKey();
    final Object entityInstance = context.getProcessingState( entityReturn ).getEntityInstance();

    if ( context.shouldReturnProxies() ) {
      final Object proxy = context.getSession().getPersistenceContext().proxyFor(
          entityReturn.getEntityPersister(),
View Full Code Here


    //    build the needed SQL.
    //   2) Determine how to read information out of the ResultSet resulting from executing the indicated SQL
    //    (the SQL aliases).  ReaderCollector and friends are where this work happens, ultimately
    //    producing a ResultSetProcessor

    final SelectStatementBuilder select = new SelectStatementBuilder( factory.getDialect() );
    final EntityReturn rootReturn = Helper.INSTANCE.extractRootReturn( loadPlan, EntityReturn.class );
    final ReaderCollectorImpl readerCollector = new ReaderCollectorImpl();

    final LoadQueryJoinAndFetchProcessor helper = new LoadQueryJoinAndFetchProcessor( aliasResolutionContext , buildingParameters, factory );

    final String[] keyColumnNamesToUse = keyColumnNames != null
        ? keyColumnNames
        : ( (Queryable) rootReturn.getEntityPersister() ).getIdentifierColumnNames();

    // LoadPlan is broken down into 2 high-level pieces that we need to process here.
    //
    // First is the QuerySpaces, which roughly equates to the SQL FROM-clause.  We'll cycle through
    // those first, generating aliases into the AliasContext in addition to writing SQL FROM-clause information
    // into SelectStatementBuilder.  The AliasContext is populated here and the reused while process the SQL
    // SELECT-clause into the SelectStatementBuilder and then again also to build the ResultSetProcessor

    processQuerySpaces(
        loadPlan.getQuerySpaces(),
        select,
        keyColumnNamesToUse,
        helper,
        aliasResolutionContext,
        buildingParameters,
        factory
    );

    // Next, we process the Returns and Fetches building the SELECT clause and at the same time building
    // Readers for reading the described results out of a SQL ResultSet

    final FetchStats fetchStats = processReturnAndFetches(
        rootReturn,
        select,
        helper,
        readerCollector,
        aliasResolutionContext
    );

    LoadPlanTreePrinter.INSTANCE.logTree( loadPlan, aliasResolutionContext );

    this.sqlStatement = select.toStatementString();
    this.resultSetProcessor = new ResultSetProcessorImpl(
        loadPlan,
        readerCollector.buildRowReader(),
        fetchStats.hasSubselectFetches()
    );
View Full Code Here

        wrapper = executeQueryStatement( sql, queryParameters, false, afterLoadActions, session );
        results = loadQueryDetails.getResultSetProcessor().extractResults(
            wrapper.getResultSet(),
            session,
            queryParameters,
            new NamedParameterContext() {
              @Override
              public int[] getNamedParameterLocations(String name) {
                return AbstractLoadPlanBasedEntityLoader.this.getNamedParameterLocs( name );
              }
            },
View Full Code Here

    try {
      final List results = loadQueryDetails.getResultSetProcessor().extractResults(
          wrapper.getResultSet(),
          session,
          queryParameters,
          new NamedParameterContext() {
            @Override
            public int[] getNamedParameterLocations(String name) {
              return AbstractLoadPlanBasedEntityLoader.this.getNamedParameterLocs( name );
            }
          },
View Full Code Here

      return new EntityLoader(
          persister.getFactory(),
          persister,
          keyColumnNames,
          keyType,
          new QueryBuildingParameters() {
            @Override
            public LoadQueryInfluencers getQueryInfluencers() {
              return influencers;
            }
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

      // do not render the entity join for a one-to-many association, since the collection join
      // already joins to the associated entity table (see doc in renderCollectionJoin()).
      if ( join.getLeftHandSide().getDisposition() == QuerySpace.Disposition.COLLECTION &&
           CollectionQuerySpace.class.cast( join.getLeftHandSide() ).getCollectionPersister().isManyToMany() ) {
        final CollectionQuerySpace leftHandSide = (CollectionQuerySpace) join.getLeftHandSide();
        final CollectionReferenceAliases aliases = aliasResolutionContext.resolveCollectionReferenceAliases(
            leftHandSide.getUid()
        );

        renderManyToManyJoin(aliases, leftHandSide,  join, joinFragment );
      }
View Full Code Here

      throw new IllegalStateException( "QuerySpace with that UID was not yet registered in the AliasResolutionContext" );
    }
    final String[] aliasedLhsColumnNames = join.resolveAliasedLeftHandSideJoinConditionColumns( leftHandSideTableAlias );

    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()
          )
      );
    }

    renderSqlJoinToCollectionTable(
View Full Code Here

      CollectionFetch 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 EntityReferenceAliases() {
        @Override
        public String getTableAlias() {
          return aliases.getCollectionTableAlias();
        }

        @Override
        public EntityAliases getColumnAliases() {
          return aliases.getEntityElementColumnAliases();
        }
      };
      aliasResolutionContext.registerQuerySpaceAliases( fetch.getQuerySpaceUid(), entityReferenceAliases );
      readerCollector.add(
          new EntityReferenceInitializerImpl(
              (EntityReference) fetch.getElementGraph(),
              entityReferenceAliases
          )
      );
    }
    else {
      final String rhsTableAlias = aliases.getElementTableAlias();

      // 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();
          }
        };
        aliasResolutionContext.registerQuerySpaceAliases( fetch.getQuerySpaceUid(), entityReferenceAliases );
        readerCollector.add(
            new EntityReferenceInitializerImpl(
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

TOP

Related Classes of org.hibernate.loader.plan2.exec.process.internal.EntityReferenceInitializerImpl

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.