Package org.hibernate

Examples of org.hibernate.QueryException


    SQLLoadable persister = context.getEntityPersisterByAlias( aliasName );
    String suffix = context.getEntitySuffixByAlias( aliasName );

    if ( "*".equals( propertyName ) ) {
      if( !fieldResults.isEmpty() ) {
        throw new QueryException("Using return-propertys together with * syntax is not supported.");
      }     
      aliasesFound++;
      return persister.selectFragment( aliasName, suffix ) ;
    }
    else {

      String[] columnAliases;

      // Let return-propertys override whatever the persister has for aliases.
      columnAliases = (String[]) fieldResults.get( propertyName );
      if ( columnAliases == null ) {
        columnAliases = persister.getSubclassPropertyColumnAliases( propertyName, suffix );
      }

      if ( columnAliases == null || columnAliases.length == 0 ) {
        throw new QueryException(
            "No column name found for property [" + propertyName + "] for alias [" + aliasName + "]",
            originalQueryString
        );
      }
      if ( columnAliases.length != 1 ) {
        // TODO: better error message since we actually support composites if names are explicitly listed.
        throw new QueryException(
            "SQL queries only support properties mapped to a single column - property [" + propertyName + "] is mapped to " + columnAliases.length + " columns.",
            originalQueryString
        );
      }     
      aliasesFound++;
View Full Code Here


          if ( fromElement.getRealOrigin() == null ) {
            // work around that crazy issue where the tree contains
            // "empty" FromElements (no text); afaict, this is caused
            // by FromElementFactory.createCollectionJoin()
            if ( fromElement.getOrigin() == null ) {
              throw new QueryException( "Unable to determine origin of join fetch [" + fromElement.getDisplayText() + "]" );
            }
            else {
              origin = fromElement.getOrigin();
            }
          }
          else {
            origin = fromElement.getRealOrigin();
          }
          if ( !fromElementsForLoad.contains( origin ) ) {
            throw new QueryException(
                "query specified join fetching, but the owner " +
                "of the fetched association was not present in the select list " +
                "[" + fromElement.getDisplayText() + "]"
            );
          }
View Full Code Here

  public String[] toColumns(String alias, String propertyName)
      throws QueryException {

    if ( "index".equals( propertyName ) ) {
      if ( isManyToMany() ) {
        throw new QueryException( "index() function not supported for many-to-many association" );
      }
      return StringHelper.qualify( alias, indexColumnNames );
    }

    return elementPropertyMapping.toColumns( alias, propertyName );
View Full Code Here

  public String[] toColumns(String propertyName)
      throws QueryException {

    if ( "index".equals( propertyName ) ) {
      if ( isManyToMany() ) {
        throw new QueryException( "index() function not supported for many-to-many association" );
      }
      return indexColumnNames;
    }

    return elementPropertyMapping.toColumns( propertyName );
View Full Code Here

  public void token(String token, QueryTranslatorImpl q) throws QueryException {

    // start by looking for HQL keywords...
    String lcToken = token.toLowerCase();
    if ( lcToken.equals( "," ) ) {
      if ( !( expectingJoin | expectingAs ) ) throw new QueryException( "unexpected token: ," );
      expectingJoin = false;
      expectingAs = false;
    }
    else if ( lcToken.equals( "join" ) ) {
      if ( !afterJoinType ) {
        if ( !( expectingJoin | expectingAs ) ) throw new QueryException( "unexpected token: join" );
        // inner joins can be abbreviated to 'join'
        joinType = JoinFragment.INNER_JOIN;
        expectingJoin = false;
        expectingAs = false;
      }
      else {
        afterJoinType = false;
      }
    }
    else if ( lcToken.equals( "fetch" ) ) {
      if ( q.isShallowQuery() ) throw new QueryException( QueryTranslator.ERROR_CANNOT_FETCH_WITH_ITERATE );
      if ( joinType == NONE ) throw new QueryException( "unexpected token: fetch" );
      if ( joinType == JoinFragment.FULL_JOIN || joinType == JoinFragment.RIGHT_OUTER_JOIN ) {
        throw new QueryException( "fetch may only be used with inner join or left outer join" );
      }
      afterFetch = true;
    }
    else if ( lcToken.equals( "outer" ) ) {
      // 'outer' is optional and is ignored
      if ( !afterJoinType ||
          ( joinType != JoinFragment.LEFT_OUTER_JOIN && joinType != JoinFragment.RIGHT_OUTER_JOIN )
      ) {
        throw new QueryException( "unexpected token: outer" );
      }
    }
    else if ( JOIN_TYPES.containsKey( lcToken ) ) {
      if ( !( expectingJoin | expectingAs ) ) throw new QueryException( "unexpected token: " + token );
      joinType = ( ( Integer ) JOIN_TYPES.get( lcToken ) ).intValue();
      afterJoinType = true;
      expectingJoin = false;
      expectingAs = false;
    }
    else if ( lcToken.equals( "class" ) ) {
      if ( !afterIn ) throw new QueryException( "unexpected token: class" );
      if ( joinType != NONE ) throw new QueryException( "outer or full join must be followed by path expression" );
      afterClass = true;
    }
    else if ( lcToken.equals( "in" ) ) {
      if (alias == null ){
        memberDeclarations = true;
        afterMemberDeclarations = false;
      }
      else if ( !expectingIn ) {
        throw new QueryException( "unexpected token: in" );
      } else {
        afterIn = true;
        expectingIn = false;
      }
     
    }
    else if ( lcToken.equals( "as" ) ) {
      if ( !expectingAs ) throw new QueryException( "unexpected token: as" );
      afterAs = true;
      expectingAs = false;
    }
    else if ( "(".equals( token ) ){
      if( !memberDeclarations ) throw new QueryException( "unexpected token: (" );
      //TODO alias should be null here
      expectingPathExpression = true;
     
    }
    else if ( ")".equals( token ) ){
//      memberDeclarations = false;
//      expectingPathExpression = false;
      afterMemberDeclarations = true;
    }
    else {

      if ( afterJoinType ) throw new QueryException( "join expected: " + token );
      if ( expectingJoin ) throw new QueryException( "unexpected token: " + token );
      if ( expectingIn ) throw new QueryException( "in expected: " + token );

      // now anything that is not a HQL keyword

      if ( afterAs || expectingAs ) {

        // (AS is always optional, for consistency with SQL/OQL)

        // process the "new" HQL style where aliases are assigned
        // _after_ the class name or path expression ie. using
        // the AS construction

        if ( entityName != null ) {
          q.setAliasName( token, entityName );
        }
        else if(collectionName != null ) {
          q.setAliasName( token, collectionName );
        }
        else {
          throw new QueryException( "unexpected: as " + token );
        }
        afterAs = false;
        expectingJoin = true;
        expectingAs = false;
        entityName = null;
        collectionName = null;
        memberDeclarations = false;
        expectingPathExpression = false;
        afterMemberDeclarations = false;

      }
      else if ( afterIn ) {

        // process the "old" HQL style where aliases appear _first_
        // ie. using the IN or IN CLASS constructions

        if ( alias == null ) throw new QueryException( "alias not specified for: " + token );

        if ( joinType != NONE ) throw new QueryException( "outer or full join must be followed by path expression" );

        if ( afterClass ) {
          // treat it as a classname
          Queryable p = q.getEntityPersisterUsingImports( token );
          if ( p == null ) throw new QueryException( "persister not found: " + token );
          q.addFromClass( alias, p );
        }
        else {
          // treat it as a path expression
          peParser.setJoinType( JoinFragment.INNER_JOIN );
          peParser.setUseThetaStyleJoin( true );
          ParserHelper.parse( peParser, q.unalias( token ), ParserHelper.PATH_SEPARATORS, q );
          if ( !peParser.isCollectionValued() ) throw new QueryException( "path expression did not resolve to collection: " + token );
          String nm = peParser.addFromCollection( q );
          q.setAliasName( alias, nm );
        }

        alias = null;
        afterIn = false;
        afterClass = false;
        expectingJoin = true;
      }
      else if( memberDeclarations && expectingPathExpression ){
        expectingAs = true;
        peParser.setJoinType( JoinFragment.INNER_JOIN );
        peParser.setUseThetaStyleJoin( false );
        ParserHelper.parse( peParser, q.unalias( token ), ParserHelper.PATH_SEPARATORS, q );
        if ( !peParser.isCollectionValued() ) throw new QueryException( "path expression did not resolve to collection: " + token );
        collectionName = peParser.addFromCollection( q );
        expectingPathExpression = false;
        memberDeclarations = false;
      }
      else {

        // handle a path expression or class name that
        // appears at the start, in the "new" HQL
        // style or an alias that appears at the start
        // in the "old" HQL style

        Queryable p = q.getEntityPersisterUsingImports( token );
        if ( p != null ) {
          // starts with the name of a mapped class (new style)
          if ( joinType != NONE ) throw new QueryException( "outer or full join must be followed by path expression" );
          entityName = q.createNameFor( p.getEntityName() );
          q.addFromClass( entityName, p );
          expectingAs = true;
        }
        else if ( token.indexOf( '.' ) < 0 ) {
View Full Code Here

  public void end(QueryTranslatorImpl q) {
    if( afterMemberDeclarations ){
      //The exception throwned by the AST query translator contains the error token location, respensent by line and colum,
      //but it hard to get that info here.
      throw new QueryException("alias not specified for IN");
    }
   
  }
View Full Code Here

      if ( type.isEntityType() ) { //ie. a many-to-many
        String entityName = ( ( EntityType ) type ).getAssociatedEntityName();
        name = pathExpressionParser.continueFromManyToMany( entityName, element.elementColumns, q );
      }
      else {
        throw new QueryException( "illegally dereferenced collection element" );
      }
    }
    return name;
  }
View Full Code Here

    String lcToken = token.toLowerCase();

    //Cope with [,]
    if ( token.equals( "[" ) && !expectingPathContinuation ) {
      expectingPathContinuation = false;
      if ( expectingIndex == 0 ) throw new QueryException( "unexpected [" );
      return;
    }
    else if ( token.equals( "]" ) ) {
      expectingIndex--;
      expectingPathContinuation = true;
      return;
    }

    //Cope with a continued path expression (ie. ].baz)
    if ( expectingPathContinuation ) {
      boolean pathExpressionContinuesFurther = continuePathExpression( token, q );
      if ( pathExpressionContinuesFurther ) return; //NOTE: early return
    }

    //Cope with a subselect
    if ( !inSubselect && ( lcToken.equals( "select" ) || lcToken.equals( "from" ) ) ) {
      inSubselect = true;
      subselect = new StringBuffer( 20 );
    }
    if ( inSubselect && token.equals( ")" ) ) {
      bracketsSinceSelect--;

      if ( bracketsSinceSelect == -1 ) {
        QueryTranslatorImpl subq = new QueryTranslatorImpl(
                subselect.toString(),
            q.getEnabledFilters(),
            q.getFactory()
        );
        try {
          subq.compile( q );
        }
        catch ( MappingException me ) {
          throw new QueryException( "MappingException occurred compiling subquery", me );
        }
        appendToken( q, subq.getSQLString() );
        inSubselect = false;
        bracketsSinceSelect = 0;
      }
View Full Code Here

  public void end(QueryTranslatorImpl q) throws QueryException {
    if ( expectingPathContinuation ) {
      expectingPathContinuation = false;
      PathExpressionParser.CollectionElement element = pathExpressionParser.lastCollectionElement();
      if ( element.elementColumns.length != 1 ) throw new QueryException( "path expression ended in composite collection element" );
      appendToken( q, element.elementColumns[0] );
      addToCurrentJoin( element );
    }
    token( ")", q );
  }
View Full Code Here

    q.addFromJoinOnly( pathExpressionParser.getName(), joinSequence );
    try {
      addToCurrentJoin( joinSequence.toJoinFragment( q.getEnabledFilters(), true ).toWhereFragmentString() );
    }
    catch ( MappingException me ) {
      throw new QueryException( me );
    }
  }
View Full Code Here

TOP

Related Classes of org.hibernate.QueryException

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.