Search in sources :

Example 6 with QueryException

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

the class FromElementType method applyTreatAsDeclarations.

public void applyTreatAsDeclarations(Set<String> treatAsDeclarations) {
    if (treatAsDeclarations != null && !treatAsDeclarations.isEmpty()) {
        if (this.treatAsDeclarations == null) {
            this.treatAsDeclarations = new HashSet<String>();
        }
        for (String treatAsSubclassName : treatAsDeclarations) {
            try {
                EntityPersister subclassPersister = fromElement.getSessionFactoryHelper().requireClassPersister(treatAsSubclassName);
                this.treatAsDeclarations.add(subclassPersister.getEntityName());
            } catch (SemanticException e) {
                throw new QueryException("Unable to locate persister for subclass named in TREAT-AS : " + treatAsSubclassName);
            }
        }
        if (joinSequence != null) {
            joinSequence.applyTreatAsDeclarations(this.treatAsDeclarations);
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) QueryException(org.hibernate.QueryException) SemanticException(antlr.SemanticException)

Example 7 with QueryException

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

the class FromElementType method getCollectionPropertyReference.

public CollectionPropertyReference getCollectionPropertyReference(final String propertyName) {
    if (queryableCollection == null) {
        throw new QueryException("Not a collection reference");
    }
    final PropertyMapping collectionPropertyMapping;
    if (queryableCollection.isManyToMany() && queryableCollection.hasIndex() && SPECIAL_MANY2MANY_TREATMENT_FUNCTION_NAMES.contains(propertyName)) {
        collectionPropertyMapping = new SpecialManyToManyCollectionPropertyMapping();
    } else if (CollectionProperties.isCollectionProperty(propertyName)) {
        if (this.collectionPropertyMapping == null) {
            this.collectionPropertyMapping = new CollectionPropertyMapping(queryableCollection);
        }
        collectionPropertyMapping = this.collectionPropertyMapping;
    } else {
        collectionPropertyMapping = queryableCollection;
    }
    return new CollectionPropertyReference() {

        @Override
        public Type getType() {
            return collectionPropertyMapping.toType(propertyName);
        }

        @Override
        public String[] toColumns(final String tableAlias) {
            if (propertyName.equalsIgnoreCase("index")) {
                return collectionPropertyMapping.toColumns(tableAlias, propertyName);
            }
            Map enabledFilters = fromElement.getWalker().getEnabledFilters();
            String subquery = CollectionSubqueryFactory.createCollectionSubquery(joinSequence.copy().setUseThetaStyle(true), enabledFilters, collectionPropertyMapping.toColumns(tableAlias, propertyName));
            LOG.debugf("toColumns(%s,%s) : subquery = %s", tableAlias, propertyName, subquery);
            return new String[] { "(" + subquery + ")" };
        }
    };
}
Also used : QueryException(org.hibernate.QueryException) PropertyMapping(org.hibernate.persister.entity.PropertyMapping) CollectionPropertyMapping(org.hibernate.persister.collection.CollectionPropertyMapping) CollectionPropertyMapping(org.hibernate.persister.collection.CollectionPropertyMapping) Map(java.util.Map)

Example 8 with QueryException

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

the class IdentNode method resolveAsNakedComponentPropertyRefLHS.

private boolean resolveAsNakedComponentPropertyRefLHS(DotNode parent) {
    FromElement fromElement = locateSingleFromElement();
    if (fromElement == null) {
        return false;
    }
    Type componentType = getNakedPropertyType(fromElement);
    if (componentType == null) {
        throw new QueryException("Unable to resolve path [" + parent.getPath() + "], unexpected token [" + getOriginalText() + "]");
    }
    if (!componentType.isComponentType()) {
        throw new QueryException("Property '" + getOriginalText() + "' is not a component.  Use an alias to reference associations or collections.");
    }
    Type propertyType;
    String propertyPath = getText() + "." + getNextSibling().getText();
    try {
        // check to see if our "propPath" actually
        // represents a property on the persister
        propertyType = fromElement.getPropertyType(getText(), propertyPath);
    } catch (Throwable t) {
        // assume we do *not* refer to a property on the given persister
        return false;
    }
    setFromElement(fromElement);
    parent.setPropertyPath(propertyPath);
    parent.setDataType(propertyType);
    return true;
}
Also used : JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException)

Example 9 with QueryException

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

the class CastFunctionNode method resolve.

/**
	 * Called from the hql-sql grammar afterQuery the children of the CAST have been resolved.
	 *
	 * @param inSelect Is this call part of the SELECT clause?
	 */
public void resolve(boolean inSelect) {
    this.dialectCastFunction = getSessionFactoryHelper().findSQLFunction("cast");
    if (dialectCastFunction == null) {
        dialectCastFunction = CastFunction.INSTANCE;
    }
    this.expressionNode = (Node) getFirstChild();
    if (expressionNode == null) {
        throw new QueryException("Could not resolve expression to CAST");
    }
    if (SqlNode.class.isInstance(expressionNode)) {
        final Type expressionType = ((SqlNode) expressionNode).getDataType();
        if (expressionType != null) {
            if (expressionType.isEntityType()) {
                throw new QueryException("Expression to CAST cannot be an entity : " + expressionNode.getText());
            }
            if (expressionType.isComponentType()) {
                throw new QueryException("Expression to CAST cannot be a composite : " + expressionNode.getText());
            }
            if (expressionType.isCollectionType()) {
                throw new QueryException("Expression to CAST cannot be a collection : " + expressionNode.getText());
            }
        }
    }
    this.typeNode = (IdentNode) expressionNode.getNextSibling();
    if (typeNode == null) {
        throw new QueryException("Could not resolve requested type for CAST");
    }
    final String typeName = typeNode.getText();
    this.castType = getSessionFactoryHelper().getFactory().getTypeResolver().heuristicType(typeName);
    if (castType == null) {
        throw new QueryException("Could not resolve requested type for CAST : " + typeName);
    }
    if (castType.isEntityType()) {
        throw new QueryException("CAST target type cannot be an entity : " + expressionNode.getText());
    }
    if (castType.isComponentType()) {
        throw new QueryException("CAST target type cannot be a composite : " + expressionNode.getText());
    }
    if (castType.isCollectionType()) {
        throw new QueryException("CAST target type cannot be a collection : " + expressionNode.getText());
    }
    setDataType(castType);
}
Also used : QueryException(org.hibernate.QueryException) Type(org.hibernate.type.Type)

Example 10 with QueryException

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

the class PathExpressionParser method token.

public void token(String token, QueryTranslatorImpl q) throws QueryException {
    if (token != null) {
        path.append(token);
    }
    String alias = q.getPathAlias(path.toString());
    if (alias != null) {
        //reset the dotcount (but not the path)
        reset(q);
        //afterQuery reset!
        currentName = alias;
        currentPropertyMapping = q.getPropertyMapping(currentName);
        if (!ignoreInitialJoin) {
            JoinSequence ojf = q.getPathJoin(path.toString());
            try {
                //afterQuery reset!
                joinSequence.addCondition(ojf.toJoinFragment(q.getEnabledFilters(), true).toWhereFragmentString());
            } catch (MappingException me) {
                throw new QueryException(me);
            }
        // we don't need to worry about any condition in the ON clause
        // here (toFromFragmentString), since anything in the ON condition
        // is already applied to the whole query
        }
    } else if (".".equals(token)) {
        dotcount++;
    } else {
        if (dotcount == 0) {
            if (!continuation) {
                if (!q.isName(token)) {
                    throw new QueryException("undefined alias: " + token);
                }
                currentName = token;
                currentPropertyMapping = q.getPropertyMapping(currentName);
            }
        } else if (dotcount == 1) {
            if (currentName != null) {
                currentProperty = token;
            } else if (collectionName != null) {
                //processCollectionProperty(token, q.getCollectionPersister(collectionRole), collectionName);
                continuation = false;
            } else {
                throw new QueryException("unexpected");
            }
        } else {
            // dotcount>=2
            // Do the corresponding RHS
            Type propertyType = getPropertyType();
            if (propertyType == null) {
                throw new QueryException("unresolved property: " + path);
            }
            if (propertyType.isComponentType()) {
                dereferenceComponent(token);
            } else if (propertyType.isEntityType()) {
                if (!isCollectionValued()) {
                    dereferenceEntity(token, (EntityType) propertyType, q);
                }
            } else if (propertyType.isCollectionType()) {
                dereferenceCollection(token, ((CollectionType) propertyType).getRole(), q);
            } else {
                if (token != null) {
                    throw new QueryException("dereferenced: " + path);
                }
            }
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) QueryException(org.hibernate.QueryException) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) JoinSequence(org.hibernate.engine.internal.JoinSequence) 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