Package org.hibernate.persister.collection

Examples of org.hibernate.persister.collection.QueryableCollection


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

    // Add the condition to the join sequence that qualifies the indexed element.
    AST index = collectionNode.getNextSibling()// The index should be a constant, which will have been processed already.
    if ( index == null ) {
      throw new QueryException( "No index value!" );
    }

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

    // 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( index ); //TODO: used to be exprNoParens! was this needed?
    }
    catch ( RecognitionException e ) {
      throw new QueryException( e.getMessage(), e );
    }
    String expression = gen.getSQL();
    joinSequence.addCondition( collectionTableAlias + '.' + indexCols[0] + " = " + expression );

    // 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

   * @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

//      }

      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

    }
  }

  private void dereferenceCollection(String propertyName, String role, QueryTranslatorImpl q) throws QueryException {
    collectionRole = role;
    QueryableCollection collPersister = q.getCollectionPersister( role );
    String name = q.createNameForCollection( role );
    addJoin( name, collPersister.getCollectionType() );
    //if ( collPersister.hasWhere() ) join.addCondition( collPersister.getSQLWhereString(name) );
    collectionName = name;
    collectionOwnerName = currentName;
    currentName = name;
    currentProperty = propertyName;
View Full Code Here

  }

  private void prepareForIndex(QueryTranslatorImpl q) throws QueryException {

    QueryableCollection collPersister = q.getCollectionPersister( collectionRole );

    if ( !collPersister.hasIndex() ) throw new QueryException( "unindexed collection before []: " + path );
    String[] indexCols = collPersister.getIndexColumnNames();
    if ( indexCols.length != 1 ) throw new QueryException( "composite-index appears in []: " + path );
    //String[] keyCols = collPersister.getKeyColumnNames();

    JoinSequence fromJoins = new JoinSequence( q.getFactory() )
        .setUseThetaStyle( useThetaStyleJoin )
        .setRoot( collPersister, collectionName )
        .setNext( joinSequence.copy() );

    if ( !continuation ) addJoin( collectionName, collPersister.getCollectionType() );

    joinSequence.addCondition( collectionName + '.' + indexCols[0] + " = " ); //TODO: get SQL rendering out of here

    CollectionElement elem = new CollectionElement();
    elem.elementColumns = collPersister.getElementColumnNames(collectionName);
    elem.elementType = collPersister.getElementType();
    elem.isOneToMany = collPersister.isOneToMany();
    elem.alias = collectionName;
    elem.joinSequence = joinSequence;
    collectionElements.addLast( elem );
    setExpectingCollectionIndex();
View Full Code Here

      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

    return joinFragment;
  }

  private boolean isManyToManyRoot(Joinable joinable) {
    if ( joinable != null && joinable.isCollection() ) {
      QueryableCollection persister = ( QueryableCollection ) joinable;
      return persister.isManyToMany();
    }
    return false;
  }
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.