Package org.hibernate.persister.collection

Examples of org.hibernate.persister.collection.QueryableCollection


    Type type = collectionNode.getDataType();
    if ( !type.isCollectionType() ) {
      throw new SemanticException( "The [] operator cannot be applied to type " + type.toString() );
    }
    String collectionRole = ( ( CollectionType ) type ).getRole();
    QueryableCollection queryableCollection = sessionFactoryHelper.requireQueryableCollection( collectionRole );
    if ( !queryableCollection.hasIndex() ) {
      throw new QueryException( "unindexed fromElement before []: " + collectionNode.getPath() );
    }

    // Generate the inner join -- The elements need to be joined to the collection they are in.
    FromElement fromElement = collectionNode.getFromElement();
    String elementTable = fromElement.getTableAlias();
    FromClause fromClause = fromElement.getFromClause();
    String path = collectionNode.getPath();

    FromElement elem = fromClause.findCollectionJoin( path );
    if ( elem == null ) {
      FromElementFactory factory = new FromElementFactory( fromClause, fromElement, path );
      elem = factory.createCollectionElementsJoin( queryableCollection, elementTable );
      if ( log.isDebugEnabled() ) {
        log.debug( "No FROM element found for the elements of collection join path " + path
            + ", created " + elem );
      }
    }
    else {
      if ( log.isDebugEnabled() ) {
        log.debug( "FROM element found for collection join path " + path );
      }
    }

    // The 'from element' that represents the elements of the collection.
    setFromElement( fromElement );

    // Add the condition to the join sequence that qualifies the indexed element.
    AST selector = collectionNode.getNextSibling();
    if ( selector == null ) {
      throw new QueryException( "No index value!" );
    }

    // Sometimes use the element table alias, sometimes use the... umm... collection table alias (many to many)
    String collectionTableAlias = elementTable;
    if ( elem.getCollectionTableAlias() != null ) {
      collectionTableAlias = elem.getCollectionTableAlias();
    }

    // TODO: get SQL rendering out of here, create an AST for the join expressions.
    // Use the SQL generator grammar to generate the SQL text for the index expression.
    JoinSequence joinSequence = fromElement.getJoinSequence();
    String[] indexCols = queryableCollection.getIndexColumnNames();
    if ( indexCols.length != 1 ) {
      throw new QueryException( "composite-index appears in []: " + collectionNode.getPath() );
    }
    SqlGenerator gen = new SqlGenerator( getSessionFactoryHelper().getFactory() );
    try {
      gen.simpleExpr( selector ); //TODO: used to be exprNoParens! was this needed?
    }
    catch ( RecognitionException e ) {
      throw new QueryException( e.getMessage(), e );
    }
    String selectorExpression = gen.getSQL();
    joinSequence.addCondition( collectionTableAlias + '.' + indexCols[0] + " = " + selectorExpression );
    List paramSpecs = gen.getCollectedParameters();
    if ( paramSpecs != null ) {
      switch ( paramSpecs.size() ) {
        case 0 :
          // nothing to do
          break;
        case 1 :
          ParameterSpecification paramSpec = ( ParameterSpecification ) paramSpecs.get( 0 );
          paramSpec.setExpectedType( queryableCollection.getIndexType() );
          fromElement.setIndexCollectionSelectorParamSpec( paramSpec );
          break;
        default:
          fromElement.setIndexCollectionSelectorParamSpec(
              new AggregatedIndexCollectionSelectorParameterSpecifications( paramSpecs )
          );
          break;
      }
    }

    // Now, set the text for this node.  It should be the element columns.
    String[] elementColumns = queryableCollection.getElementColumnNames( elementTable );
    setText( elementColumns[0] );
    setResolved();
  }
View Full Code Here


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

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

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

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

    return excludeEmpty()
View Full Code Here

//      }

      if ( isFilter() ) {
        // Handle collection-fiter compilation.
        // IMPORTANT NOTE: This is modifying the INPUT (HQL) tree, not the output tree!
        QueryableCollection persister = sessionFactoryHelper.getCollectionPersister( collectionFilterRole );
        Type collectionElementType = persister.getElementType();
        if ( !collectionElementType.isEntityType() ) {
          throw new QueryException( "collection of values in filter: this" );
        }

        String collectionElementEntityName = persister.getElementPersister().getEntityName();
        ASTFactory inputAstFactory = hqlParser.getASTFactory();
        AST fromElement = inputAstFactory.create( FILTER_ENTITY, collectionElementEntityName );
        ASTUtil.createSibling( inputAstFactory, HqlTokenTypes.ALIAS, "this", fromElement );
        fromClauseInput.addChild( fromElement );
        // Show the modified AST.
View Full Code Here

  }

  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(),
          JoinFragment.INNER_JOIN,
          persister.getElementColumnNames( fkTableAlias ) );
    }
    join.addCondition( fkTableAlias, keyColumnNames, " = ?" );
    fromElement.setJoinSequence( join );
    fromElement.setFilter( true );
    if ( log.isDebugEnabled() ) {
View Full Code Here

   * @return The defined CollectionPersister for this collection role.
   * @throws QueryException Indicates that the collection persister could not be found.
   */
  public QueryableCollection requireQueryableCollection(String role) throws QueryException {
    try {
      QueryableCollection queryableCollection = ( QueryableCollection ) sfi.getCollectionPersister( role );
      if ( queryableCollection != null ) {
        collectionPropertyMappingByRole.put( role, new CollectionPropertyMapping( queryableCollection ) );
      }
      return queryableCollection;
    }
View Full Code Here

    }

    // TODO : most of below was taken verbatim from DotNode; should either delegate this logic or super-type it
    CollectionType type = (CollectionType) getDataType();
    String role = type.getRole();
    QueryableCollection queryableCollection = getSessionFactoryHelper().requireQueryableCollection(role);

    String alias = null// DotNode uses null here...
    String columnTableAlias = getFromElement().getTableAlias();
    int joinType = JoinFragment.INNER_JOIN;
    boolean fetch = false;

    FromElementFactory factory = new FromElementFactory(
        getWalker().getCurrentFromClause(),
        getFromElement(),
        propertyName,
        alias,
        getFromElement().toColumns(columnTableAlias, propertyName, false),
        true
    );
    FromElement elem = factory.createCollection(queryableCollection, role, joinType, fetch, true);
    setFromElement(elem);
    getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces())// Always add the collection's query spaces.
  }
View Full Code Here

    boolean isSizeProperty = getNextSibling()!=null &&
      CollectionProperties.isAnyCollectionProperty( getNextSibling().getText() );

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

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

    if ( getWalker().getStatementType() != SqlTokenTypes.SELECT && indexed && classAlias == null ) {
      // should indicate that we are processing an INSERT/UPDATE/DELETE
      // query with a subquery implied via a collection property
      // function. Here, we need to use the table name itself as the
      // qualification alias.
      // TODO : verify this works for all databases...
      // TODO : is this also the case in non-"indexed" scenarios?
      String alias = getLhs().getFromElement().getQueryable().getTableName();
      columns = getFromElement().toColumns( alias, 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 );
   
    if ( log.isDebugEnabled() ) {
      log.debug( "dereferenceCollection() : Created new FROM element for " + 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

    }
  }

  private void handleElements(FromReferenceNode collectionNode, String propertyName) {
    FromElement collectionFromElement = collectionNode.getFromElement();
    QueryableCollection queryableCollection = collectionFromElement.getQueryableCollection();

    String path = collectionNode.getPath() + "[]." + propertyName;
    log.debug( "Creating elements for " + path );

    fromElement = collectionFromElement;
    if ( !collectionFromElement.isCollectionOfValuesOrComponents() ) {
      getWalker().addQuerySpaces( queryableCollection.getElementPersister().getQuerySpaces() );
    }

    setDataType( queryableCollection.getElementType() );
    selectColumns = collectionFromElement.toColumns( fromElement.getTableAlias(), propertyName, inSelect );
  }
View Full Code Here

        includeInResultRowList.add( true );
      }
      else if ( rtn instanceof CollectionReturn ) {
        CollectionReturn collRtn = (CollectionReturn) rtn;
        String role = collRtn.getOwnerEntityName() + "." + collRtn.getOwnerProperty();
        QueryableCollection persister = (QueryableCollection) factory.getCollectionPersister( role );
        collectionPersisters.add( persister );
        lockModes.add( collRtn.getLockMode() );
        resultColumnProcessors.add( new NonScalarResultColumnProcessor( returnableCounter++ ) );
        nonScalarReturnList.add( rtn );
        collectionOwners.add( -1 );
        resultTypes.add( persister.getType() );
        specifiedAliases.add( collRtn.getAlias() );
        collectionAliases.add( collRtn.getCollectionAliases() );
        // determine if the collection elements are entities...
        Type elementType = persister.getElementType();
        if ( elementType.isEntityType() ) {
          Queryable elementPersister = (Queryable) ((EntityType) elementType).getAssociatedJoinable( factory );
          entityPersisters.add( elementPersister );
          entityOwners.add( -1 );
          entityAliases.add( collRtn.getElementEntityAliases() );
          ArrayHelper.addAll( querySpaces, elementPersister.getQuerySpaces() );
        }
        includeInResultRowList.add( true );
      }
      else if ( rtn instanceof EntityFetchReturn ) {
        EntityFetchReturn fetchRtn = (EntityFetchReturn) rtn;
        NonScalarReturn ownerDescriptor = fetchRtn.getOwner();
        int ownerIndex = nonScalarReturnList.indexOf( ownerDescriptor );
        entityOwners.add( ownerIndex );
        lockModes.add( fetchRtn.getLockMode() );
        Queryable ownerPersister = determineAppropriateOwnerPersister( ownerDescriptor );
        EntityType fetchedType = (EntityType) ownerPersister.getPropertyType( fetchRtn.getOwnerProperty() );
        String entityName = fetchedType.getAssociatedEntityName( getFactory() );
        Queryable persister = (Queryable) factory.getEntityPersister( entityName );
        entityPersisters.add( persister );
        nonScalarReturnList.add( rtn );
        specifiedAliases.add( fetchRtn.getAlias() );
        entityAliases.add( fetchRtn.getEntityAliases() );
        ArrayHelper.addAll( querySpaces, persister.getQuerySpaces() );
        includeInResultRowList.add( false );
      }
      else if ( rtn instanceof CollectionFetchReturn ) {
        CollectionFetchReturn fetchRtn = (CollectionFetchReturn) rtn;
        NonScalarReturn ownerDescriptor = fetchRtn.getOwner();
        int ownerIndex = nonScalarReturnList.indexOf( ownerDescriptor );
        collectionOwners.add( ownerIndex );
        lockModes.add( fetchRtn.getLockMode() );
        Queryable ownerPersister = determineAppropriateOwnerPersister( ownerDescriptor );
        String role = ownerPersister.getEntityName() + '.' + fetchRtn.getOwnerProperty();
        QueryableCollection persister = (QueryableCollection) factory.getCollectionPersister( role );
        collectionPersisters.add( persister );
        nonScalarReturnList.add( rtn );
        specifiedAliases.add( fetchRtn.getAlias() );
        collectionAliases.add( fetchRtn.getCollectionAliases() );
        // determine if the collection elements are entities...
        Type elementType = persister.getElementType();
        if ( elementType.isEntityType() ) {
          Queryable elementPersister = (Queryable) ((EntityType) elementType).getAssociatedJoinable( factory );
          entityPersisters.add( elementPersister );
          entityOwners.add( ownerIndex );
          entityAliases.add( fetchRtn.getElementEntityAliases() );
View Full Code Here

  private TypeDiscriminatorMetadata buildTypeDiscriminatorMetadata() {
    final String aliasToUse = getTableAlias();
    Queryable queryable = getQueryable();
    if ( queryable == null ) {
      QueryableCollection collection = getQueryableCollection();
      if ( ! collection.getElementType().isEntityType() ) {
        throw new QueryException( "type discrimination cannot be applied to value collection [" + collection.getRole() + "]" );
      }
      queryable = (Queryable) collection.getElementPersister();
    }

    handlePropertyBeingDereferenced( getDataType(), DISCRIMINATOR_PROPERTY_NAME );

    return new TypeDiscriminatorMetadataImpl( queryable.getTypeDiscriminatorMetadata(), aliasToUse );
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.