Search in sources :

Example 61 with QueryException

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

the class BulkManipulationTest method testSimpleInsertTypeMismatchException.

@Test
public void testSimpleInsertTypeMismatchException() {
    Session s = openSession();
    try {
        org.hibernate.Query q = s.createQuery("insert into Pickup (id, owner, vin) select id, :owner, id from Car");
        fail("Parameter type mismatch but no exception thrown");
    } catch (Throwable throwable) {
        QueryException queryException = assertTyping(QueryException.class, throwable.getCause());
        String m = queryException.getMessage();
        // insertion type [org.hibernate.type.StringType@21e3cc77] and selection type [org.hibernate.type.LongType@7284aa02] at position 2 are not compatible [insert into Pickup (id, owner, vin) select id, :owner, id from org.hibernate.test.hql.Car]
        int st = m.indexOf("org.hibernate.type.StringType");
        int lt = m.indexOf("org.hibernate.type.LongType");
        assertTrue("type causing error not reported", st > -1);
        assertTrue("type causing error not reported", lt > -1);
        assertTrue(lt > st);
        assertTrue("wrong position of type error reported", m.indexOf("position 2") > -1);
    } finally {
        s.close();
    }
}
Also used : QueryException(org.hibernate.QueryException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 62 with QueryException

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

the class CaseStatementTest method testSimpleCaseStatementWithParamAllResults.

@Test
public void testSimpleCaseStatementWithParamAllResults() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    try {
        s.createQuery("select case p.name when 'Steve' then :opt1 else :opt2 end from Person p").setString("opt1", "x").setString("opt2", "y").list();
        fail("was expecting an exception");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException expected) {
    // expected
    }
    s.createQuery("select case p.name when 'Steve' then cast( :opt1 as string ) else cast( :opt2 as string) end from Person p").setString("opt1", "x").setString("opt2", "y").list();
    t.commit();
    s.close();
}
Also used : QueryException(org.hibernate.QueryException) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 63 with QueryException

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

the class HQLTest method prepareTest.

@Override
protected void prepareTest() throws Exception {
    super.prepareTest();
    SelectClause.VERSION2_SQL = true;
    DotNode.regressionStyleJoinSuppression = true;
    DotNode.ILLEGAL_COLL_DEREF_EXCP_BUILDER = new DotNode.IllegalCollectionDereferenceExceptionBuilder() {

        public QueryException buildIllegalCollectionDereferenceException(String propertyName, FromReferenceNode lhs) {
            throw new QueryException("illegal syntax near collection: " + propertyName);
        }
    };
    SqlGenerator.REGRESSION_STYLE_CROSS_JOINS = true;
}
Also used : FromReferenceNode(org.hibernate.hql.internal.ast.tree.FromReferenceNode) DotNode(org.hibernate.hql.internal.ast.tree.DotNode) QueryException(org.hibernate.QueryException)

Example 64 with QueryException

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

the class PathExpressionParser method prepareForIndex.

private void prepareForIndex(QueryTranslatorImpl q) throws QueryException {
    QueryableCollection collPersister = q.getCollectionPersister(collectionRole);
    if (!collPersister.hasIndex()) {
        throw new QueryException("unindexed collection beforeQuery []: " + path);
    }
    String[] indexCols = collPersister.getIndexColumnNames();
    if (indexCols.length != 1) {
        throw new QueryException("composite-index appears in []: " + path);
    }
    //String[] keyCols = collPersister.getKeyColumnNames();
    JoinSequence fromJoins = new JoinSequence(q.getFactory()).setUseThetaStyle(useThetaStyleJoin).setRoot(collPersister, collectionName).setNext(joinSequence.copy());
    if (!continuation) {
        addJoin(collectionName, collPersister.getCollectionType());
    }
    //TODO: get SQL rendering out of here
    joinSequence.addCondition(collectionName + '.' + indexCols[0] + " = ");
    CollectionElement elem = new CollectionElement();
    elem.elementColumns = collPersister.getElementColumnNames(collectionName);
    elem.elementType = collPersister.getElementType();
    elem.isOneToMany = collPersister.isOneToMany();
    elem.alias = collectionName;
    elem.joinSequence = joinSequence;
    collectionElements.addLast(elem);
    setExpectingCollectionIndex();
    q.addCollection(collectionName, collectionRole);
    q.addFromJoinOnly(collectionName, fromJoins);
}
Also used : QueryException(org.hibernate.QueryException) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 65 with QueryException

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

the class PathExpressionParser method getPropertyType.

protected Type getPropertyType() throws QueryException {
    String propertyPath = getPropertyPath();
    Type propertyType = getPropertyMapping().toType(propertyPath);
    if (propertyType == null) {
        throw new QueryException("could not resolve property type: " + propertyPath);
    }
    return propertyType;
}
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)

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