Package org.hibernate.persister.collection

Examples of org.hibernate.persister.collection.QueryableCollection


      throw new QueryException( "must specify 'elements' for collection valued property in from clause: " + path );
    }

    if ( collectionElementType.isEntityType() ) {
      // an association
      QueryableCollection collectionPersister = q.getCollectionPersister( collectionRole );
      Queryable entityPersister = ( Queryable ) collectionPersister.getElementPersister();
      String clazz = entityPersister.getEntityName();

      final String elementName;
      if ( collectionPersister.isOneToMany() ) {
        elementName = collectionName;
        //allow index() function:
        q.decoratePropertyMapping( elementName, collectionPersister );
      }
      else { //many-to-many
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 = ASTUtil.create( inputAstFactory, HqlTokenTypes.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

   * 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,
            JoinFragment.INNER_JOIN,
            persister.getElementColumnNames(collectionName) );
      }
      catch ( MappingException me ) {
        throw new QueryException( me );
      }
    }
View Full Code Here

        ArrayHelper.addAll( querySpaces, persister.getQuerySpaces() );
      }
      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( new Integer( -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( new Integer( -1 ) );
          entityAliases.add( collRtn.getElementEntityAliases() );
          ArrayHelper.addAll( querySpaces, elementPersister.getQuerySpaces() );
        }
      }
      else if ( rtn instanceof EntityFetchReturn ) {
        EntityFetchReturn fetchRtn = ( EntityFetchReturn ) rtn;
        NonScalarReturn ownerDescriptor = fetchRtn.getOwner();
        int ownerIndex = nonScalarReturnList.indexOf( ownerDescriptor );
        entityOwners.add( new Integer( 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() );
      }
      else if ( rtn instanceof CollectionFetchReturn ) {
        CollectionFetchReturn fetchRtn = ( CollectionFetchReturn ) rtn;
        NonScalarReturn ownerDescriptor = fetchRtn.getOwner();
        int ownerIndex = nonScalarReturnList.indexOf( ownerDescriptor );
        collectionOwners.add( new Integer( 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( new Integer( ownerIndex ) );
          entityAliases.add( fetchRtn.getElementEntityAliases() );
View Full Code Here

    OuterJoinableAssociation last = null;
    while ( iter.hasNext() ) {
      OuterJoinableAssociation oj = (OuterJoinableAssociation) iter.next();
      if ( oj.getJoinType() == JoinFragment.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()==JoinFragment.LEFT_OUTER_JOIN ) {
          //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();
          i++;
        }
      }
    }
View Full Code Here

  public void prepareForDot(String propertyName) throws SemanticException {
    FromElement fromElement = getFromElement();
    if ( fromElement == null ) {
      throw new IllegalStateException( "No FROM element for index operator!" );
    }
    QueryableCollection queryableCollection = fromElement.getQueryableCollection();
    if ( queryableCollection != null && !queryableCollection.isOneToMany() ) {

      FromReferenceNode collectionNode = ( FromReferenceNode ) getFirstChild();
      String path = collectionNode.getPath() + "[]." + propertyName;
      if ( log.isDebugEnabled() ) {
        log.debug( "Creating join for many-to-many elements for " + path );
View Full Code Here

    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

  public void prepareForDot(String propertyName) throws SemanticException {
    FromElement fromElement = getFromElement();
    if ( fromElement == null ) {
      throw new IllegalStateException( "No FROM element for index operator!" );
    }
    QueryableCollection queryableCollection = fromElement.getQueryableCollection();
    if ( queryableCollection != null && !queryableCollection.isOneToMany() ) {

      FromReferenceNode collectionNode = ( FromReferenceNode ) getFirstChild();
      String path = collectionNode.getPath() + "[]." + propertyName;
      if ( log.isDebugEnabled() ) {
        log.debug( "Creating join for many-to-many elements for " + path );
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.