Search in sources :

Example 21 with QueryException

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

the class QueryParameterBindingsImpl method verifyParametersBound.

public void verifyParametersBound(boolean reserveFirstParameter) {
    // verify named parameters bound
    for (Map.Entry<QueryParameter, QueryParameterBinding> bindEntry : parameterBindingMap.entrySet()) {
        if (!bindEntry.getValue().isBound()) {
            if (bindEntry.getKey().getName() != null) {
                throw new QueryException("Named parameter [" + bindEntry.getKey().getName() + "] not set");
            } else {
                throw new QueryException("Parameter memento [" + bindEntry.getKey() + "] not set");
            }
        }
    }
    // verify position parameters bound
    int startIndex = 0;
    if (!parameterMetadata.isOrdinalParametersZeroBased()) {
        startIndex = 1;
    }
    for (int i = startIndex; i < positionalParameterBindings.size(); i++) {
        QueryParameterBinding binding = null;
        if (parameterMetadata.isOrdinalParametersZeroBased()) {
            binding = positionalParameterBindings.get(i);
        } else {
            binding = positionalParameterBindings.get(i - 1);
        }
        if (binding == null || !binding.isBound()) {
            throw new QueryException("Positional parameter [" + i + "] not set");
        }
    }
    // verify position parameter count is correct
    final int positionalValueSpan = calculatePositionalValueSpan(reserveFirstParameter);
    final int positionCounts = parameterMetadata.getPositionalParameterCount();
    if (positionCounts != positionalValueSpan) {
        if (reserveFirstParameter && positionCounts - 1 != positionalValueSpan) {
            throw new QueryException("Expected positional parameter count: " + (positionCounts - 1) + ", actually detected " + positionalValueSpan);
        } else if (!reserveFirstParameter) {
            throw new QueryException("Expected positional parameter count: " + (positionCounts) + ", actually detected " + positionalValueSpan);
        }
    }
}
Also used : QueryParameter(org.hibernate.query.QueryParameter) QueryException(org.hibernate.QueryException) QueryParameterBinding(org.hibernate.query.spi.QueryParameterBinding) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 22 with QueryException

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

the class ASTParserLoadingTest method testFetchInSubqueryFails.

@Test
public void testFetchInSubqueryFails() {
    Session s = openSession();
    try {
        s.createQuery("from Animal a where a.mother in (select m from Animal a1 inner join a1.mother as m join fetch m.mother)").list();
        fail("fetch join allowed in subquery");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException expected) {
    // expected behavior
    }
    s.close();
}
Also used : QueryException(org.hibernate.QueryException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with QueryException

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

the class ASTParserLoadingTest method testInvalidCollectionDereferencesFail.

@Test
public void testInvalidCollectionDereferencesFail() {
    Session s = openSession();
    s.beginTransaction();
    // control group...
    s.createQuery("from Animal a join a.offspring o where o.description = 'xyz'").list();
    s.createQuery("from Animal a join a.offspring o where o.father.description = 'xyz'").list();
    s.createQuery("from Animal a join a.offspring o order by o.description").list();
    s.createQuery("from Animal a join a.offspring o order by o.father.description").list();
    try {
        s.createQuery("from Animal a where a.offspring.description = 'xyz'").list();
        fail("illegal collection dereference semantic did not cause failure");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException qe) {
        log.trace("expected failure...", qe);
    }
    try {
        s.createQuery("from Animal a where a.offspring.father.description = 'xyz'").list();
        fail("illegal collection dereference semantic did not cause failure");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException qe) {
        log.trace("expected failure...", qe);
    }
    try {
        s.createQuery("from Animal a order by a.offspring.description").list();
        fail("illegal collection dereference semantic did not cause failure");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException qe) {
        log.trace("expected failure...", qe);
    }
    try {
        s.createQuery("from Animal a order by a.offspring.father.description").list();
        fail("illegal collection dereference semantic did not cause failure");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException qe) {
        log.trace("expected failure...", qe);
    }
    s.getTransaction().commit();
    s.close();
}
Also used : QueryException(org.hibernate.QueryException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 24 with QueryException

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

the class BulkManipulationTest method testSelectWithNamedParamProjection.

@Test
public void testSelectWithNamedParamProjection() {
    Session s = openSession();
    try {
        s.createQuery("select :someParameter, id from Car");
        fail("Should throw an unsupported exception");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException q) {
    // allright
    } finally {
        s.close();
    }
}
Also used : QueryException(org.hibernate.QueryException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 25 with QueryException

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

the class BulkManipulationTest method testInsertIntoSuperclassPropertiesFails.

@Test
public void testInsertIntoSuperclassPropertiesFails() {
    TestData data = new TestData();
    data.prepare();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    try {
        s.createQuery("insert into Human (id, bodyWeight) select id, bodyWeight from Lizard").executeUpdate();
        fail("superclass prop insertion did not error");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException e) {
    // expected result
    }
    t.commit();
    t = s.beginTransaction();
    s.createQuery("delete Animal where mother is not null").executeUpdate();
    s.createQuery("delete Animal where father is not null").executeUpdate();
    s.createQuery("delete Animal").executeUpdate();
    t.commit();
    s.close();
    data.cleanup();
}
Also used : QueryException(org.hibernate.QueryException) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

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