Search in sources :

Example 11 with QueryException

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

the class QueryTranslatorImpl method compile.

/**
	 * Compile the query (generate the SQL).
	 *
	 * @throws org.hibernate.MappingException Indicates problems resolving
	 * things referenced in the query.
	 * @throws org.hibernate.QueryException Generally some form of syntatic
	 * failure.
	 */
private void compile() throws QueryException, MappingException {
    LOG.trace("Compiling query");
    try {
        ParserHelper.parse(new PreprocessingParser(tokenReplacements), queryString, ParserHelper.HQL_SEPARATORS, this);
        renderSQL();
    } catch (QueryException qe) {
        if (qe.getQueryString() == null) {
            throw qe.wrapWithQueryString(queryString);
        } else {
            throw qe;
        }
    } catch (MappingException me) {
        throw me;
    } catch (Exception e) {
        LOG.debug("Unexpected query compilation problem", e);
        e.printStackTrace();
        throw new QueryException("Incorrect query syntax", queryString, e);
    }
    postInstantiate();
    compiled = true;
}
Also used : QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) SQLException(java.sql.SQLException) MappingException(org.hibernate.MappingException)

Example 12 with QueryException

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

the class PathExpressionParser method dereferenceEntity.

private void dereferenceEntity(String propertyName, EntityType propertyType, QueryTranslatorImpl q) throws QueryException {
    //NOTE: we avoid joining to the next table if the named property is just the foreign key value
    //if its "id"
    boolean isIdShortcut = EntityPersister.ENTITY_ID.equals(propertyName) && propertyType.isReferenceToPrimaryKey();
    //or its the id property name
    final String idPropertyName;
    try {
        idPropertyName = propertyType.getIdentifierOrUniqueKeyPropertyName(q.getFactory());
    } catch (MappingException me) {
        throw new QueryException(me);
    }
    boolean isNamedIdPropertyShortcut = idPropertyName != null && idPropertyName.equals(propertyName) && propertyType.isReferenceToPrimaryKey();
    if (isIdShortcut || isNamedIdPropertyShortcut) {
        // this must only occur at the _end_ of a path expression
        if (componentPath.length() > 0) {
            componentPath.append('.');
        }
        componentPath.append(propertyName);
    } else {
        String entityClass = propertyType.getAssociatedEntityName();
        String name = q.createNameFor(entityClass);
        q.addType(name, entityClass);
        addJoin(name, propertyType);
        if (propertyType.isOneToOne()) {
            oneToOneOwnerName = currentName;
        }
        ownerAssociationType = propertyType;
        currentName = name;
        currentProperty = propertyName;
        q.addPathAliasAndJoin(path.substring(0, path.toString().lastIndexOf('.')), name, joinSequence.copy());
        componentPath.setLength(0);
        currentPropertyMapping = q.getEntityPersister(entityClass);
    }
}
Also used : QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException)

Example 13 with QueryException

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

the class PathExpressionParser method addFromCollection.

public String addFromCollection(QueryTranslatorImpl q) throws QueryException {
    Type collectionElementType = getPropertyType();
    if (collectionElementType == null) {
        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
            q.addCollection(collectionName, collectionRole);
            elementName = q.createNameFor(clazz);
            addJoin(elementName, (AssociationType) collectionElementType);
        }
        q.addFrom(elementName, clazz, joinSequence);
        currentPropertyMapping = new CollectionPropertyMapping(collectionPersister);
        return elementName;
    } else {
        // collections of values
        q.addFromCollection(collectionName, collectionRole, joinSequence);
        return collectionName;
    }
}
Also used : 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) QueryException(org.hibernate.QueryException) Queryable(org.hibernate.persister.entity.Queryable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) CollectionPropertyMapping(org.hibernate.persister.collection.CollectionPropertyMapping)

Example 14 with QueryException

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

the class LiteralNode method determineConvertedValue.

@SuppressWarnings("unchecked")
protected String determineConvertedValue(AttributeConverterTypeAdapter converterTypeAdapter, Object literalValue) {
    if (getDataType().getReturnedClass().equals(converterTypeAdapter.getModelType())) {
        // apply the converter
        final AttributeConverter converter = converterTypeAdapter.getAttributeConverter();
        final Object converted = converter.convertToDatabaseColumn(getLiteralValue());
        if (isCharacterData(converterTypeAdapter.sqlType())) {
            return "'" + converted.toString() + "'";
        } else {
            return converted.toString();
        }
    } else if (getDataType().getReturnedClass().equals(converterTypeAdapter.getJdbcType())) {
        if (isCharacterData(converterTypeAdapter.sqlType())) {
            return "'" + literalValue.toString() + "'";
        } else {
            return literalValue.toString();
        }
    } else {
        throw new QueryException(String.format(Locale.ROOT, "AttributeConverter domain-model attribute type [%s] and JDBC type [%s] did not match query literal type [%s]", converterTypeAdapter.getModelType().getName(), converterTypeAdapter.getJdbcType().getName(), getDataType().getReturnedClass().getName()));
    }
}
Also used : AttributeConverter(javax.persistence.AttributeConverter) QueryException(org.hibernate.QueryException)

Example 15 with QueryException

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

the class WhereParser method token.

public void token(String token, QueryTranslatorImpl q) throws QueryException {
    String lcToken = token.toLowerCase(Locale.ROOT);
    //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) {
            //NOTE: early return
            return;
        }
    }
    //Cope with a subselect
    if (!inSubselect && (lcToken.equals("select") || lcToken.equals("from"))) {
        inSubselect = true;
        subselect = new StringBuilder(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;
        }
    }
    if (inSubselect) {
        if (token.equals("(")) {
            bracketsSinceSelect++;
        }
        subselect.append(token).append(' ');
        return;
    }
    //Cope with special cases of AND, NOT, ()
    specialCasesBefore(lcToken);
    //Close extra brackets we opened
    if (!betweenSpecialCase && EXPRESSION_TERMINATORS.contains(lcToken)) {
        closeExpression(q, lcToken);
    }
    //take note when this is a boolean expression
    if (BOOLEAN_OPERATORS.contains(lcToken)) {
        booleanTests.removeLast();
        booleanTests.addLast(Boolean.TRUE);
    }
    if (lcToken.equals("not")) {
        nots.addLast(!(nots.removeLast()));
        negated = !negated;
        //NOTE: early return
        return;
    }
    //process a token, mapping OO path expressions to SQL expressions
    doToken(token, q);
    //Open any extra brackets we might need.
    if (!betweenSpecialCase && EXPRESSION_OPENERS.contains(lcToken)) {
        openExpression(q, lcToken);
    }
    //Cope with special cases of AND, NOT, )
    specialCasesAfter(lcToken);
}
Also used : QueryException(org.hibernate.QueryException) 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