Search in sources :

Example 51 with QueryException

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

the class ExceptionConverterImpl method convert.

@Override
public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
    Throwable cause = e;
    if (cause instanceof StaleStateException) {
        final PersistenceException converted = wrapStaleStateException((StaleStateException) cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof LockingStrategyException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.exception.LockTimeoutException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.PessimisticLockException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.QueryTimeoutException) {
        final QueryTimeoutException converted = new QueryTimeoutException(cause.getMessage(), cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof ObjectNotFoundException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueObjectException) {
        final EntityExistsException converted = new EntityExistsException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueResultException) {
        final NonUniqueResultException converted = new NonUniqueResultException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof UnresolvableObjectException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof QueryException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof MultipleBagFetchException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof TransientObjectException) {
        try {
            sharedSessionContract.markForRollbackOnly();
        } catch (Exception ne) {
            //we do not want the subsequent exception to swallow the original one
            log.unableToMarkForRollbackOnTransientObjectException(ne);
        }
        //Spec 3.2.3 Synchronization rules
        return new IllegalStateException(e);
    } else {
        final PersistenceException converted = new PersistenceException(cause);
        handlePersistenceException(converted);
        return converted;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransientObjectException(org.hibernate.TransientObjectException) HibernateException(org.hibernate.HibernateException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) NoResultException(javax.persistence.NoResultException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) SQLException(java.sql.SQLException) NonUniqueResultException(javax.persistence.NonUniqueResultException) JDBCException(org.hibernate.JDBCException) EntityNotFoundException(javax.persistence.EntityNotFoundException) StaleStateException(org.hibernate.StaleStateException) OptimisticEntityLockException(org.hibernate.dialect.lock.OptimisticEntityLockException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityExistsException(javax.persistence.EntityExistsException) OptimisticLockException(javax.persistence.OptimisticLockException) StaleObjectStateException(org.hibernate.StaleObjectStateException) PessimisticLockException(javax.persistence.PessimisticLockException) PessimisticEntityLockException(org.hibernate.dialect.lock.PessimisticEntityLockException) TransientObjectException(org.hibernate.TransientObjectException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) QueryTimeoutException(javax.persistence.QueryTimeoutException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) QueryTimeoutException(javax.persistence.QueryTimeoutException) QueryException(org.hibernate.QueryException) StaleStateException(org.hibernate.StaleStateException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) PersistenceException(javax.persistence.PersistenceException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException)

Example 52 with QueryException

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

the class StandardCallableStatementSupport method renderCallableStatement.

@Override
public String renderCallableStatement(String procedureName, ParameterStrategy parameterStrategy, List<ParameterRegistrationImplementor<?>> parameterRegistrations, SharedSessionContractImplementor session) {
    final StringBuilder buffer = new StringBuilder().append("{call ").append(procedureName).append("(");
    String sep = "";
    for (ParameterRegistrationImplementor parameter : parameterRegistrations) {
        if (parameter == null) {
            throw new QueryException("Parameter registrations had gaps");
        }
        if (parameter.getMode() == ParameterMode.REF_CURSOR) {
            verifyRefCursorSupport(session.getJdbcServices().getJdbcEnvironment().getDialect());
            buffer.append(sep).append("?");
            sep = ",";
        } else {
            for (int i = 0; i < parameter.getSqlTypes().length; i++) {
                buffer.append(sep).append("?");
                sep = ",";
            }
        }
    }
    return buffer.append(")}").toString();
}
Also used : QueryException(org.hibernate.QueryException) ParameterRegistrationImplementor(org.hibernate.procedure.spi.ParameterRegistrationImplementor)

Example 53 with QueryException

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

the class ASTParserLoadingTest method testInvalidFetchSemantics.

@Test
public void testInvalidFetchSemantics() {
    Session s = openSession();
    s.beginTransaction();
    try {
        s.createQuery("select mother from Human a left join fetch a.mother mother").list();
        fail("invalid fetch semantic allowed!");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException e) {
    }
    try {
        s.createQuery("select mother from Human a left join fetch a.mother mother").list();
        fail("invalid fetch semantic allowed!");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException e) {
    }
    s.getTransaction().commit();
    s.close();
}
Also used : QueryException(org.hibernate.QueryException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 54 with QueryException

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

the class ASTParserLoadingTest method testIllegalMixedTransformerQueries.

@Test
public void testIllegalMixedTransformerQueries() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    try {
        getSelectNewQuery(session).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
        fail("'select new' together with a resulttransformer should result in error!");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException he) {
        assertTrue(he.getMessage().indexOf("ResultTransformer") == 0);
    }
    try {
        getSelectNewQuery(session).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).iterate();
        fail("'select new' together with a resulttransformer should result in error!");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (HibernateException he) {
        assertTrue(he.getMessage().indexOf("ResultTransformer") == 0);
    }
    try {
        getSelectNewQuery(session).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).scroll();
        fail("'select new' together with a resulttransformer should result in error!");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (HibernateException he) {
        assertTrue(he.getMessage().indexOf("ResultTransformer") == 0);
    }
    t.commit();
    session.close();
}
Also used : QueryException(org.hibernate.QueryException) Transaction(org.hibernate.Transaction) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 55 with QueryException

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

the class FooBarTest method testParameterCheck.

@Test
public void testParameterCheck() throws HibernateException {
    Session s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.x > :myX");
        q.list();
        fail("Should throw QueryException for missing myX");
    } catch (QueryException iae) {
    // should happen
    } finally {
        s.close();
    }
    s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.x > ?");
        q.list();
        fail("Should throw QueryException for missing ?");
    } catch (QueryException iae) {
    // should happen
    } finally {
        s.close();
    }
    s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.x > ? or bar.short = 1 or bar.string = 'ff ? bb'");
        q.setInteger(0, 1);
        q.list();
    } catch (QueryException iae) {
        fail("Should not throw QueryException for missing ?");
    } finally {
        s.close();
    }
    s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.string = ' ? ' or bar.string = '?'");
        q.list();
    } catch (QueryException iae) {
        fail("Should not throw QueryException for ? in quotes");
    } finally {
        s.close();
    }
    s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.string = ? or bar.string = ? or bar.string = ?");
        q.setParameter(0, "bull");
        q.setParameter(2, "shit");
        q.list();
        fail("should throw exception telling me i have not set parameter 1");
    } catch (QueryException iae) {
    // should happen!
    } finally {
        s.close();
    }
}
Also used : QueryException(org.hibernate.QueryException) Query(org.hibernate.Query) 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