Package org.hibernate.persister.collection

Examples of org.hibernate.persister.collection.QueryableCollection


  @Override
  protected AST createFromFilterElement(AST filterEntity, AST alias) throws SemanticException {
    FromElement fromElement = currentFromClause.addFromElement( filterEntity.getText(), alias );
    FromClause fromClause = fromElement.getFromClause();
    QueryableCollection persister = sessionFactoryHelper.getCollectionPersister( collectionFilterRole );
    // Get the names of the columns used to link between the collection
    // owner and the collection elements.
    String[] keyColumnNames = persister.getKeyColumnNames();
    String fkTableAlias = persister.isOneToMany()
        ? fromElement.getTableAlias()
        : fromClause.getAliasGenerator().createName( collectionFilterRole );
    JoinSequence join = sessionFactoryHelper.createJoinSequence();
    join.setRoot( persister, fkTableAlias );
    if ( !persister.isOneToMany() ) {
      join.addJoin(
          (AssociationType) persister.getElementType(),
          fromElement.getTableAlias(),
          JoinType.INNER_JOIN,
          persister.getElementColumnNames( fkTableAlias )
      );
    }
    join.addCondition( fkTableAlias, keyColumnNames, " = ?" );
    fromElement.setJoinSequence( join );
    fromElement.setFilter( true );
View Full Code Here


    final String entityName = criteriaQuery.getEntityName( criteria, propertyName );
    final String actualPropertyName = criteriaQuery.getPropertyName( propertyName );
    final String sqlAlias = criteriaQuery.getSQLAlias( criteria, propertyName );

    final SessionFactoryImplementor factory = criteriaQuery.getFactory();
    final QueryableCollection collectionPersister = getQueryableCollection( entityName, actualPropertyName, factory );

    final String[] collectionKeys = collectionPersister.getKeyColumnNames();
    final String[] ownerKeys = ( (Loadable) factory.getEntityPersister( entityName ) ).getIdentifierColumnNames();

    final String innerSelect = "(select 1 from " + collectionPersister.getTableName() + " where "
        + new ConditionFragment().setTableAlias( sqlAlias ).setCondition( ownerKeys, collectionKeys ).toFragmentString()
        + ")";

    return excludeEmpty()
        ? "exists " + innerSelect
View Full Code Here

    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

    OuterJoinableAssociation last = null;
    while ( iter.hasNext() ) {
      OuterJoinableAssociation oj = (OuterJoinableAssociation) iter.next();
      if ( oj.getJoinType() == JoinType.LEFT_OUTER_JOIN ) { // why does this matter?
        if ( oj.getJoinable().isCollection() ) {
          final QueryableCollection queryableCollection = (QueryableCollection) oj.getJoinable();
          if ( queryableCollection.hasOrdering() ) {
            final String orderByString = queryableCollection.getSQLOrderByString( oj.getRHSAlias() );
            buf.append( orderByString ).append(", ");
          }
        }
        else {
          // it might still need to apply a collection ordering based on a
          // many-to-many defined order-by...
          if ( last != null && last.getJoinable().isCollection() ) {
            final QueryableCollection queryableCollection = (QueryableCollection) last.getJoinable();
            if ( queryableCollection.isManyToMany() && last.isManyToManyWith( oj ) ) {
              if ( queryableCollection.hasManyToManyOrdering() ) {
                final String orderByString = queryableCollection.getManyToManyOrderByString( oj.getRHSAlias() );
                buf.append( orderByString ).append(", ");
              }
            }
          }
        }
View Full Code Here

        i++;
       
      }
      else {
       
        QueryableCollection collPersister = (QueryableCollection) oj.getJoinable();
        if ( oj.getJoinType()==JoinType.LEFT_OUTER_JOIN && ! oj.hasRestriction() ) {
          //it must be a collection fetch
          collectionPersisters[j] = collPersister;
          collectionOwners[j] = oj.getOwner(associations);
          j++;
        }
 
        if ( collPersister.isOneToMany() ) {
          persisters[i] = (Loadable) collPersister.getElementPersister();
          aliases[i] = oj.getRHSAlias();
          callback.associationProcessed( oj, i );
          i++;
        }
      }
View Full Code Here

        collectionPersister.getElementColumnNames()
    );
  }

  public static Iterable<AttributeDefinition> getCompositeCollectionIndexSubAttributes(CompositeCollectionElementDefinition compositionElementDefinition){
    final QueryableCollection collectionPersister =
        (QueryableCollection) compositionElementDefinition.getCollectionDefinition().getCollectionPersister();
    return getSingularSubAttributes(
        compositionElementDefinition.getSource(),
        (OuterJoinLoadable) collectionPersister.getOwnerEntityPersister(),
        (CompositeType) collectionPersister.getIndexType(),
        collectionPersister.getTableName(),
        collectionPersister.getIndexColumnNames()
    );
  }
View Full Code Here

          String.format( "Non-Map attribute [%s] cannot be target of key subgraph", getAttributeName() )
      );
    }

    final AssociationType attributeType = (AssociationType) Helper.resolveType( sessionFactory(), attribute );
    final QueryableCollection collectionPersister = (QueryableCollection) attributeType.getAssociatedJoinable( sessionFactory() );
    final Type indexType = collectionPersister.getIndexType();

    if ( ! indexType.isAssociationType() ) {
      throw new IllegalArgumentException(
          String.format( "Map index [%s] is not an entity; cannot be target of key subgraph", getAttributeName() )
      );
View Full Code Here

   * Used for collection filters
   */
  private void addFromAssociation(final String elementName, final String collectionRole)
      throws QueryException {
    //q.addCollection(collectionName, collectionRole);
    QueryableCollection persister = getCollectionPersister( collectionRole );
    Type collectionElementType = persister.getElementType();
    if ( !collectionElementType.isEntityType() ) {
      throw new QueryException( "collection of values in filter: " + elementName );
    }

    String[] keyColumnNames = persister.getKeyColumnNames();
    //if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);

    String collectionName;
    JoinSequence join = new JoinSequence( getFactory() );
    collectionName = persister.isOneToMany() ?
        elementName :
        createNameForCollection( collectionRole );
    join.setRoot( persister, collectionName );
    if ( !persister.isOneToMany() ) {
      //many-to-many
      addCollection( collectionName, collectionRole );
      try {
        join.addJoin( ( AssociationType ) persister.getElementType(),
            elementName,
            JoinType.INNER_JOIN,
            persister.getElementColumnNames(collectionName) );
      }
      catch ( MappingException me ) {
        throw new QueryException( me );
      }
    }
View Full Code Here

    final FromReferenceNode mapReference = getMapReference();
    mapReference.resolve( true, true );

    FromElement sourceFromElement = null;
    if ( isAliasRef( mapReference ) ) {
      final QueryableCollection collectionPersister = mapReference.getFromElement().getQueryableCollection();
      if ( Map.class.isAssignableFrom( collectionPersister.getCollectionType().getReturnedClass() ) ) {
        sourceFromElement = mapReference.getFromElement();
      }
    }
    else {
      if ( mapReference.getDataType().isCollectionType() ) {
View Full Code Here

    if ( isSizeProperty ) {
      indexed = true; //yuck!
    }

    QueryableCollection queryableCollection = getSessionFactoryHelper().requireQueryableCollection( role );
    String propName = getPath();
    FromClause currentFromClause = getWalker().getCurrentFromClause();

    // determine whether we should use the table name or table alias to qualify the column names...
    // we need to use the table-name when:
    //    1) the top-level statement is not a SELECT
    //    2) the LHS FromElement is *the* FromElement from the top-level statement
    //
    // there is a caveat here.. if the update/delete statement are "multi-table" we should continue to use
    // the alias also, even if the FromElement is the root one...
    //
    // in all other cases, we should use the table alias
    final FromElement lhsFromElement = getLhs().getFromElement();
    if ( getWalker().getStatementType() != SqlTokenTypes.SELECT ) {
      if ( isFromElementUpdateOrDeleteRoot( lhsFromElement ) ) {
        // at this point we know we have the 2 conditions above,
        // lets see if we have the mentioned "multi-table" caveat...
        boolean useAlias = false;
        if ( getWalker().getStatementType() != SqlTokenTypes.INSERT ) {
          final Queryable persister = lhsFromElement.getQueryable();
          if ( persister.isMultiTable() ) {
            useAlias = true;
          }
        }
        if ( !useAlias ) {
          final String lhsTableName = lhsFromElement.getQueryable().getTableName();
          columns = getFromElement().toColumns( lhsTableName, propertyPath, false, true );
        }
      }
    }

    // We do not look for an existing join on the same path, because
    // it makes sense to join twice on the same collection role
    FromElementFactory factory = new FromElementFactory(
        currentFromClause,
        getLhs().getFromElement(),
        propName,
        classAlias,
        getColumns(),
        implicitJoin
    );
    FromElement elem = factory.createCollection( queryableCollection, role, joinType, fetch, indexed );

    LOG.debugf( "dereferenceCollection() : Created new FROM element for %s : %s", propName, elem );

    setImpliedJoin( elem );
    setFromElement( elem );    // This 'dot' expression now refers to the resulting from element.

    if ( isSizeProperty ) {
      elem.setText( "" );
      elem.setUseWhereFragment( false );
    }

    if ( !implicitJoin ) {
      EntityPersister entityPersister = elem.getEntityPersister();
      if ( entityPersister != null ) {
        getWalker().addQuerySpaces( entityPersister.getQuerySpaces() );
      }
    }
    getWalker().addQuerySpaces( queryableCollection.getCollectionSpaces() );    // Always add the collection's query spaces.
  }
View Full Code Here

TOP

Related Classes of org.hibernate.persister.collection.QueryableCollection

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.