Package org.hibernate.persister.collection

Examples of org.hibernate.persister.collection.QueryableCollection


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


  }

  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

    FromElement fromElement = getFromElement();
    if ( fromElement == null ) {
      throw new IllegalStateException( "No FROM element for index operator!" );
    }

    final QueryableCollection queryableCollection = fromElement.getQueryableCollection();
    if ( queryableCollection != null && !queryableCollection.isOneToMany() ) {
      final FromReferenceNode collectionNode = (FromReferenceNode) getFirstChild();
      final String path = collectionNode.getPath() + "[]." + propertyName;
      LOG.debugf( "Creating join for many-to-many elements for %s", path );
      final FromElementFactory factory = new FromElementFactory( fromElement.getFromClause(), fromElement, path );
      // This will add the new from element to the origin.
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 );
      LOG.debugf( "No FROM element found for the elements of collection join path %s, created %s", path, elem );
    }
    else {
      LOG.debugf( "FROM element found for collection join path %s", 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<ParameterSpecification> paramSpecs = gen.getCollectedParameters();
    if ( paramSpecs != null ) {
      switch ( paramSpecs.size() ) {
        case 0:
          // nothing to do
          break;
        case 1:
          ParameterSpecification paramSpec = 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

            final FromElementFactory fromElementFactory = new FromElementFactory(
                fromClause, origin,
                attributeName, classAlias, columns, false
            );
            final QueryableCollection queryableCollection = walker.getSessionFactoryHelper()
                .requireQueryableCollection( collectionType.getRole() );
            fromElement = fromElementFactory.createCollection(
                queryableCollection, collectionType.getRole(), JoinType.LEFT_OUTER_JOIN, true, false
            );
          }
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

    else if ( collectionElementType.isComponentType() ) {
      visitCompositeDefinition( elementDefinition.toCompositeElementDefinition() );
    }
    else if ( collectionElementType.isEntityType() ) {
      if ( ! collectionDefinition.getCollectionPersister().isOneToMany() ) {
        final QueryableCollection queryableCollection = (QueryableCollection) collectionDefinition.getCollectionPersister();
        addAssociationKey(
            new AssociationKey(
                queryableCollection.getTableName(),
                queryableCollection.getElementColumnNames()
            )
        );
      }
      visitEntityDefinition( elementDefinition.toEntityDefinition() );
    }
View Full Code Here

    if ( type.getForeignKeyDirection() == ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT ) {
      final String lhsTableName;
      final String[] lhsColumnNames;

      if ( joinable.isCollection() ) {
        final QueryableCollection collectionPersister = (QueryableCollection) joinable;
        lhsTableName = collectionPersister.getTableName();
        lhsColumnNames = collectionPersister.getElementColumnNames();
      }
      else {
        final OuterJoinLoadable entityPersister = (OuterJoinLoadable) source();
        lhsTableName = getLHSTableName( type, attributeNumber(), entityPersister );
        lhsColumnNames = getLHSColumnNames( type, attributeNumber(), entityPersister, sessionFactory() );
View Full Code Here

  @Override
  public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    final String entityName =criteriaQuery.getEntityName( criteria, propertyName );
    final String role = entityName + '.' + criteriaQuery.getPropertyName( propertyName );
    final QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister( role );

    final String[] fk = cp.getKeyColumnNames();
    final String[] pk = ( (Loadable) cp.getOwnerEntityPersister() ).getIdentifierColumnNames();

    final ConditionFragment subQueryRestriction = new ConditionFragment()
        .setTableAlias( criteriaQuery.getSQLAlias( criteria, propertyName ) )
        .setCondition( pk, fk );

    return String.format(
        "? %s (select count(*) from %s where %s)",
        op,
        cp.getTableName(),
        subQueryRestriction.toFragmentString()
    );
  }
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.