Search in sources :

Example 46 with QueryException

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

the class HqlSqlWalker method createFromJoinElement.

@Override
protected void createFromJoinElement(AST path, AST alias, int joinType, AST fetchNode, AST propertyFetch, AST with) throws SemanticException {
    boolean fetch = fetchNode != null;
    if (fetch && isSubQuery()) {
        throw new QueryException("fetch not allowed in subquery from-elements");
    }
    // the incoming "path" can be either:
    //		1) an implicit join path (join p.address.city)
    // 		2) an entity-join (join com.acme.User)
    //
    // so make the proper interpretation here...
    final EntityPersister entityJoinReferencedPersister = resolveEntityJoinReferencedPersister(path);
    if (entityJoinReferencedPersister != null) {
        // `path` referenced an entity
        final EntityJoinFromElement join = createEntityJoin(entityJoinReferencedPersister, alias, joinType, propertyFetch, with);
        ((FromReferenceNode) path).setFromElement(join);
    } else {
        if (path.getType() != SqlTokenTypes.DOT) {
            throw new SemanticException("Path expected for join!");
        }
        DotNode dot = (DotNode) path;
        JoinType hibernateJoinType = JoinProcessor.toHibernateJoinType(joinType);
        // Tell the dot node about the join type.
        dot.setJoinType(hibernateJoinType);
        dot.setFetch(fetch);
        // Generate an explicit join for the root dot node.   The implied joins will be collected and passed up
        // to the root dot node.
        dot.resolve(true, false, alias == null ? null : alias.getText());
        final FromElement fromElement;
        if (dot.getDataType() != null && dot.getDataType().isComponentType()) {
            if (dot.getDataType().isAnyType()) {
                throw new SemanticException("An AnyType attribute cannot be join fetched");
            // ^^ because the discriminator (aka, the "meta columns") must be known to the SQL in
            // 		a non-parameterized way.
            }
            FromElementFactory factory = new FromElementFactory(getCurrentFromClause(), dot.getLhs().getFromElement(), dot.getPropertyPath(), alias == null ? null : alias.getText(), null, false);
            fromElement = factory.createComponentJoin((CompositeType) dot.getDataType());
        } else {
            fromElement = dot.getImpliedJoin();
            fromElement.setAllPropertyFetch(propertyFetch != null);
            if (with != null) {
                if (fetch) {
                    throw new SemanticException("with-clause not allowed on fetched associations; use filters");
                }
                handleWithFragment(fromElement, with);
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("createFromJoinElement() : " + getASTPrinter().showAsString(fromElement, "-- join tree --"));
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) FromReferenceNode(org.hibernate.hql.internal.ast.tree.FromReferenceNode) QueryException(org.hibernate.QueryException) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) DotNode(org.hibernate.hql.internal.ast.tree.DotNode) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) JoinType(org.hibernate.sql.JoinType) FromElementFactory(org.hibernate.hql.internal.ast.tree.FromElementFactory) SemanticException(antlr.SemanticException) CompositeType(org.hibernate.type.CompositeType)

Example 47 with QueryException

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

the class CriteriaQueryTranslator method getPathInfo.

private CriteriaInfoProvider getPathInfo(String path) {
    StringTokenizer tokens = new StringTokenizer(path, ".");
    String componentPath = "";
    // start with the 'rootProvider'
    CriteriaInfoProvider provider = nameCriteriaInfoMap.get(rootEntityName);
    while (tokens.hasMoreTokens()) {
        componentPath += tokens.nextToken();
        final Type type = provider.getType(componentPath);
        if (type.isAssociationType()) {
            // CollectionTypes are always also AssociationTypes - but there's not always an associated entity...
            final AssociationType atype = (AssociationType) type;
            final CollectionType ctype = type.isCollectionType() ? (CollectionType) type : null;
            final Type elementType = (ctype != null) ? ctype.getElementType(sessionFactory) : null;
            // is the association a collection of components or value-types? (i.e a colloction of valued types?)
            if (ctype != null && elementType.isComponentType()) {
                provider = new ComponentCollectionCriteriaInfoProvider(helper.getCollectionPersister(ctype.getRole()));
            } else if (ctype != null && !elementType.isEntityType()) {
                provider = new ScalarCollectionCriteriaInfoProvider(helper, ctype.getRole());
            } else {
                provider = new EntityCriteriaInfoProvider((Queryable) sessionFactory.getEntityPersister(atype.getAssociatedEntityName(sessionFactory)));
            }
            componentPath = "";
        } else if (type.isComponentType()) {
            if (!tokens.hasMoreTokens()) {
                throw new QueryException("Criteria objects cannot be created directly on components.  Create a criteria on " + "owning entity and use a dotted property to access component property: " + path);
            } else {
                componentPath += '.';
            }
        } else {
            throw new QueryException("not an association: " + componentPath);
        }
    }
    return provider;
}
Also used : StringTokenizer(java.util.StringTokenizer) StringRepresentableType(org.hibernate.type.StringRepresentableType) CollectionType(org.hibernate.type.CollectionType) JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) AssociationType(org.hibernate.type.AssociationType) CollectionType(org.hibernate.type.CollectionType)

Example 48 with QueryException

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

the class SQLQueryParser method resolveProperties.

private String resolveProperties(String aliasName, String propertyName) {
    Map fieldResults = context.getPropertyResultsMapByAlias(aliasName);
    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++;
        return columnAliases[0];
    }
}
Also used : QueryException(org.hibernate.QueryException) SQLLoadable(org.hibernate.persister.entity.SQLLoadable) Map(java.util.Map) HashMap(java.util.HashMap)

Example 49 with QueryException

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

the class QueryTranslatorTestCase method assertTranslation.

protected void assertTranslation(String hql, Map replacements, boolean scalar, String sql) {
    SessionFactoryImplementor factory = sessionFactory();
    // Create an empty replacements map if we don't have one.
    if (replacements == null) {
        replacements = new HashMap();
    }
    // steve -> note that the empty maps here represent the currently enabled filters...
    QueryTranslator oldQueryTranslator = null;
    Exception oldException = null;
    try {
        System.out.println("Compiling with classic QueryTranslator...");
        QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
        oldQueryTranslator = classic.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
        oldQueryTranslator.compile(replacements, scalar);
    } catch (QueryException e) {
        oldException = e;
    } catch (MappingException e) {
        oldException = e;
    }
    QueryTranslator newQueryTranslator = null;
    Exception newException = null;
    try {
        System.out.println("Compiling with AST QueryTranslator...");
        newQueryTranslator = createNewQueryTranslator(hql, replacements, scalar);
    } catch (QueryException e) {
        newException = e;
    } catch (MappingException e) {
        newException = e;
    }
    // If the old QT threw an exception, the new one should too.
    if (oldException != null) {
        assertNotNull("New query translator did *NOT* throw an exception, the old one did : " + oldException, newException);
        assertEquals(oldException.getMessage(), newException.getMessage());
        // Don't bother with the rest of the assertions.
        return;
    } else if (newException != null) {
        newException.printStackTrace();
        assertNull("Old query translator did not throw an exception, the new one did", newException);
    }
    // -- check all of the outputs --
    checkSql(oldQueryTranslator, newQueryTranslator, hql, scalar, sql);
    checkQuerySpaces(oldQueryTranslator, newQueryTranslator);
    checkReturnedTypes(oldQueryTranslator, newQueryTranslator);
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) QueryException(org.hibernate.QueryException) HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException)

Example 50 with QueryException

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

the class QueryTranslatorTestCase method compileBadHql.

protected Exception compileBadHql(String hql, boolean scalar) {
    QueryTranslator newQueryTranslator;
    Map replacements = null;
    Exception newException = null;
    SessionFactoryImplementor factory = sessionFactory();
    try {
        QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
        newQueryTranslator = ast.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
        newQueryTranslator.compile(replacements, scalar);
    } catch (QueryException e) {
        newException = e;
    } catch (MappingException e) {
        newException = e;
    }
    assertNotNull("Expected exception from compilation of '" + hql + "'!", newException);
    return newException;
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) QueryException(org.hibernate.QueryException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) MappingException(org.hibernate.MappingException)

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