Search in sources :

Example 41 with QueryException

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

the class FromElement method buildTypeDiscriminatorMetadata.

private TypeDiscriminatorMetadata buildTypeDiscriminatorMetadata() {
    final String aliasToUse = getTableAlias();
    Queryable queryable = getQueryable();
    if (queryable == null) {
        QueryableCollection collection = getQueryableCollection();
        if (!collection.getElementType().isEntityType()) {
            throw new QueryException("type discrimination cannot be applied to value collection [" + collection.getRole() + "]");
        }
        queryable = (Queryable) collection.getElementPersister();
    }
    handlePropertyBeingDereferenced(getDataType(), DISCRIMINATOR_PROPERTY_NAME);
    return new TypeDiscriminatorMetadataImpl(queryable.getTypeDiscriminatorMetadata(), aliasToUse);
}
Also used : QueryException(org.hibernate.QueryException) Queryable(org.hibernate.persister.entity.Queryable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 42 with QueryException

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

the class IntoClause method validateTypes.

public void validateTypes(SelectClause selectClause) throws QueryException {
    Type[] selectTypes = selectClause.getQueryReturnTypes();
    if (selectTypes.length + selectClause.getTotalParameterCount() != types.length) {
        throw new QueryException("number of select types did not match those for insert");
    }
    int parameterCount = 0;
    for (int i = 0; i < types.length; i++) {
        if (selectClause.getParameterPositions().contains(i)) {
            parameterCount++;
        } else if (!areCompatible(types[i], selectTypes[i - parameterCount])) {
            throw new QueryException("insertion type [" + types[i] + "] and selection type [" + selectTypes[i - parameterCount] + "] at position " + i + " are not compatible");
        }
    }
// otherwise, everything ok.
}
Also used : CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException)

Example 43 with QueryException

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

the class JavaConstantNode method getRenderText.

@Override
@SuppressWarnings("unchecked")
public String getRenderText(SessionFactoryImplementor sessionFactory) {
    final Type type = expectedType == null ? heuristicType : Number.class.isAssignableFrom(heuristicType.getReturnedClass()) ? heuristicType : expectedType;
    try {
        if (LiteralType.class.isInstance(type)) {
            final LiteralType literalType = (LiteralType) type;
            final Dialect dialect = factory.getDialect();
            return literalType.objectToSQLString(constantValue, dialect);
        } else if (AttributeConverterTypeAdapter.class.isInstance(type)) {
            final AttributeConverterTypeAdapter converterType = (AttributeConverterTypeAdapter) type;
            if (!converterType.getModelType().isInstance(constantValue)) {
                throw new QueryException(String.format(Locale.ENGLISH, "Recognized query constant expression [%s] was not resolved to type [%s] expected by defined AttributeConverter [%s]", constantExpression, constantValue.getClass().getName(), converterType.getModelType().getName()));
            }
            final Object value = converterType.getAttributeConverter().convertToDatabaseColumn(constantValue);
            if (String.class.equals(converterType.getJdbcType())) {
                return "'" + value + "'";
            } else {
                return value.toString();
            }
        } else {
            throw new QueryException(String.format(Locale.ENGLISH, "Unrecognized Hibernate Type for handling query constant (%s); expecting LiteralType implementation or AttributeConverter", constantExpression));
        }
    } catch (QueryException e) {
        throw e;
    } catch (Exception t) {
        throw new QueryException(QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + constantExpression, t);
    }
}
Also used : LiteralType(org.hibernate.type.LiteralType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) Dialect(org.hibernate.dialect.Dialect) LiteralType(org.hibernate.type.LiteralType) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) QueryException(org.hibernate.QueryException)

Example 44 with QueryException

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

the class HqlSqlWalker method isNonQualifiedPropertyRef.

@Override
protected boolean isNonQualifiedPropertyRef(AST ident) {
    final String identText = ident.getText();
    if (currentFromClause.isFromElementAlias(identText)) {
        return false;
    }
    List fromElements = currentFromClause.getExplicitFromElements();
    if (fromElements.size() == 1) {
        final FromElement fromElement = (FromElement) fromElements.get(0);
        try {
            LOG.tracev("Attempting to resolve property [{0}] as a non-qualified ref", identText);
            return fromElement.getPropertyMapping(identText).toType(identText) != null;
        } catch (QueryException e) {
        // Should mean that no such property was found
        }
    }
    return false;
}
Also used : QueryException(org.hibernate.QueryException) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) List(java.util.List) ArrayList(java.util.ArrayList)

Example 45 with QueryException

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

the class AssignmentSpecification method getSqlAssignmentFragment.

public String getSqlAssignmentFragment() {
    if (sqlAssignmentString == null) {
        try {
            SqlGenerator sqlGenerator = new SqlGenerator(factory);
            sqlGenerator.comparisonExpr(eq, false);
            // false indicates to not generate parens around the assignment
            sqlAssignmentString = sqlGenerator.getSQL();
        } catch (Throwable t) {
            throw new QueryException("cannot interpret set-clause assignment");
        }
    }
    return sqlAssignmentString;
}
Also used : QueryException(org.hibernate.QueryException) SqlGenerator(org.hibernate.hql.internal.ast.SqlGenerator)

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