Search in sources :

Example 66 with QueryException

use of org.hibernate.QueryException in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method getEntityPersisterForName.

private Queryable getEntityPersisterForName(String name) throws QueryException {
    String type = getType(name);
    Queryable persister = getEntityPersister(type);
    if (persister == null) {
        throw new QueryException("persistent class not found: " + type);
    }
    return persister;
}
Also used : QueryException(org.hibernate.QueryException) Queryable(org.hibernate.persister.entity.Queryable)

Example 67 with QueryException

use of org.hibernate.QueryException in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method getPropertyMapping.

PropertyMapping getPropertyMapping(String name) throws QueryException {
    PropertyMapping decorator = getDecoratedPropertyMapping(name);
    if (decorator != null) {
        return decorator;
    }
    String type = getType(name);
    if (type == null) {
        String role = getRole(name);
        if (role == null) {
            throw new QueryException("alias not found: " + name);
        }
        //.getElementPropertyMapping();
        return getCollectionPersister(role);
    } else {
        Queryable persister = getEntityPersister(type);
        if (persister == null) {
            throw new QueryException("persistent class not found: " + type);
        }
        return persister;
    }
}
Also used : QueryException(org.hibernate.QueryException) Queryable(org.hibernate.persister.entity.Queryable) PropertyMapping(org.hibernate.persister.entity.PropertyMapping)

Example 68 with QueryException

use of org.hibernate.QueryException in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method addFromAssociation.

/**
	 * 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, JoinType.INNER_JOIN, persister.getElementColumnNames(collectionName));
        } catch (MappingException me) {
            throw new QueryException(me);
        }
    }
    join.addCondition(collectionName, keyColumnNames, " = ?");
    //if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
    EntityType elemType = (EntityType) collectionElementType;
    addFrom(elementName, elemType.getAssociatedEntityName(), join);
}
Also used : EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) EntityType(org.hibernate.type.EntityType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence) MappingException(org.hibernate.MappingException)

Example 69 with QueryException

use of org.hibernate.QueryException in project hibernate-orm by hibernate.

the class SelectParser method token.

public void token(String token, QueryTranslatorImpl q) throws QueryException {
    String lctoken = token.toLowerCase(Locale.ROOT);
    if (first) {
        first = false;
        if ("distinct".equals(lctoken)) {
            q.setDistinct(true);
            return;
        } else if ("all".equals(lctoken)) {
            q.setDistinct(false);
            return;
        }
    }
    if (afterNew) {
        afterNew = false;
        try {
            holderClass = q.getFactory().getServiceRegistry().getService(ClassLoaderService.class).classForName(QuerySplitter.getImportedClass(token, q.getFactory()));
        } catch (ClassLoadingException e) {
            throw new QueryException(e);
        }
        if (holderClass == null) {
            throw new QueryException("class not found: " + token);
        }
        q.setHolderClass(holderClass);
        insideNew = true;
    } else if (token.equals(",")) {
        if (!aggregate && ready) {
            throw new QueryException("alias or expression expected in SELECT");
        }
        q.appendScalarSelectToken(", ");
        ready = true;
    } else if ("new".equals(lctoken)) {
        afterNew = true;
        ready = false;
    } else if ("(".equals(token)) {
        if (insideNew && !aggregate && !ready) {
            //opening paren in new Foo ( ... )
            ready = true;
        } else if (aggregate) {
            q.appendScalarSelectToken(token);
        } else {
            throw new QueryException("aggregate function expected beforeQuery ( in SELECT");
        }
        ready = true;
    } else if (")".equals(token)) {
        if (insideNew && !aggregate && !ready) {
            //if we are inside a new Result(), but not inside a nested function
            insideNew = false;
        } else if (aggregate && ready) {
            q.appendScalarSelectToken(token);
            aggregateFuncTokenList.removeLast();
            if (aggregateFuncTokenList.size() < 1) {
                aggregate = false;
                ready = false;
            }
        } else {
            throw new QueryException("( expected beforeQuery ) in select");
        }
    } else if (COUNT_MODIFIERS.contains(lctoken)) {
        if (!ready || !aggregate) {
            throw new QueryException(token + " only allowed inside aggregate function in SELECT");
        }
        q.appendScalarSelectToken(token);
        if ("*".equals(token)) {
            // special case
            q.addSelectScalar(getFunction("count", q).getReturnType(StandardBasicTypes.LONG, q.getFactory()));
        }
    } else if (getFunction(lctoken, q) != null && token.equals(q.unalias(token))) {
        // the name of an SQL function
        if (!ready) {
            throw new QueryException(", expected beforeQuery aggregate function in SELECT: " + token);
        }
        aggregate = true;
        aggregateAddSelectScalar = true;
        aggregateFuncTokenList.add(lctoken);
        ready = false;
        q.appendScalarSelectToken(token);
        if (!aggregateHasArgs(lctoken, q)) {
            q.addSelectScalar(aggregateType(aggregateFuncTokenList, null, q));
            if (!aggregateFuncNoArgsHasParenthesis(lctoken, q)) {
                aggregateFuncTokenList.removeLast();
                if (aggregateFuncTokenList.size() < 1) {
                    aggregate = false;
                    ready = false;
                } else {
                    ready = true;
                }
            }
        }
    } else if (aggregate) {
        boolean constantToken = false;
        if (!ready) {
            throw new QueryException("( expected afterQuery aggregate function in SELECT");
        }
        try {
            ParserHelper.parse(aggregatePathExpressionParser, q.unalias(token), ParserHelper.PATH_SEPARATORS, q);
        } catch (QueryException qex) {
            constantToken = true;
        }
        if (constantToken) {
            q.appendScalarSelectToken(token);
        } else {
            if (aggregatePathExpressionParser.isCollectionValued()) {
                q.addCollection(aggregatePathExpressionParser.getCollectionName(), aggregatePathExpressionParser.getCollectionRole());
            }
            q.appendScalarSelectToken(aggregatePathExpressionParser.getWhereColumn());
            if (aggregateAddSelectScalar) {
                q.addSelectScalar(aggregateType(aggregateFuncTokenList, aggregatePathExpressionParser.getWhereColumnType(), q));
                aggregateAddSelectScalar = false;
            }
            aggregatePathExpressionParser.addAssociation(q);
        }
    } else {
        if (!ready) {
            throw new QueryException(", expected in SELECT");
        }
        ParserHelper.parse(pathExpressionParser, q.unalias(token), ParserHelper.PATH_SEPARATORS, q);
        if (pathExpressionParser.isCollectionValued()) {
            q.addCollection(pathExpressionParser.getCollectionName(), pathExpressionParser.getCollectionRole());
        } else if (pathExpressionParser.getWhereColumnType().isEntityType()) {
            q.addSelectClass(pathExpressionParser.getSelectName());
        }
        q.appendScalarSelectTokens(pathExpressionParser.getWhereColumns());
        q.addSelectScalar(pathExpressionParser.getWhereColumnType());
        pathExpressionParser.addAssociation(q);
        ready = false;
    }
}
Also used : QueryException(org.hibernate.QueryException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException)

Example 70 with QueryException

use of org.hibernate.QueryException in project hibernate-orm by hibernate.

the class WhereParser method getElementName.

private String getElementName(PathExpressionParser.CollectionElement element, QueryTranslatorImpl q) throws QueryException {
    String name;
    if (element.isOneToMany) {
        name = element.alias;
    } else {
        Type type = element.elementType;
        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;
}
Also used : EntityType(org.hibernate.type.EntityType) LiteralType(org.hibernate.type.LiteralType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException)

Aggregations

QueryException (org.hibernate.QueryException)77 Type (org.hibernate.type.Type)20 Test (org.junit.Test)19 Session (org.hibernate.Session)18 Transaction (org.hibernate.Transaction)12 JoinType (org.hibernate.sql.JoinType)12 Queryable (org.hibernate.persister.entity.Queryable)10 CollectionType (org.hibernate.type.CollectionType)10 MappingException (org.hibernate.MappingException)9 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)9 AssociationType (org.hibernate.type.AssociationType)8 HashMap (java.util.HashMap)6 HibernateException (org.hibernate.HibernateException)6 JoinSequence (org.hibernate.engine.internal.JoinSequence)6 EntityType (org.hibernate.type.EntityType)6 AST (antlr.collections.AST)5 Map (java.util.Map)5 SemanticException (antlr.SemanticException)4 ArrayList (java.util.ArrayList)4 FromElement (org.hibernate.hql.internal.ast.tree.FromElement)4