Search in sources :

Example 1 with SQLGrammarException

use of org.hibernate.exception.SQLGrammarException in project hibernate-orm by hibernate.

the class SQLStateConversionDelegate method convert.

@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
    final String sqlState = JdbcExceptionHelper.extractSqlState(sqlException);
    final int errorCode = JdbcExceptionHelper.extractErrorCode(sqlException);
    if (sqlState != null) {
        String sqlStateClassCode = JdbcExceptionHelper.determineSqlStateClassCode(sqlState);
        if (sqlStateClassCode != null) {
            if (SQL_GRAMMAR_CATEGORIES.contains(sqlStateClassCode)) {
                return new SQLGrammarException(message, sqlException, sql);
            } else if (INTEGRITY_VIOLATION_CATEGORIES.contains(sqlStateClassCode)) {
                final String constraintName = getConversionContext().getViolatedConstraintNameExtracter().extractConstraintName(sqlException);
                return new ConstraintViolationException(message, sqlException, sql, constraintName);
            } else if (CONNECTION_CATEGORIES.contains(sqlStateClassCode)) {
                return new JDBCConnectionException(message, sqlException, sql);
            } else if (DATA_CATEGORIES.contains(sqlStateClassCode)) {
                return new DataException(message, sqlException, sql);
            }
        }
        if ("40001".equals(sqlState)) {
            return new LockAcquisitionException(message, sqlException, sql);
        }
        if ("40XL1".equals(sqlState) || "40XL2".equals(sqlState)) {
            // Derby "A lock could not be obtained within the time requested."
            return new PessimisticLockException(message, sqlException, sql);
        }
        // MySQL Query execution was interrupted
        if ("70100".equals(sqlState) || // Oracle user requested cancel of current operation
        ("72000".equals(sqlState) && errorCode == 1013)) {
            throw new QueryTimeoutException(message, sqlException, sql);
        }
    }
    return null;
}
Also used : DataException(org.hibernate.exception.DataException) QueryTimeoutException(org.hibernate.QueryTimeoutException) JDBCConnectionException(org.hibernate.exception.JDBCConnectionException) SQLGrammarException(org.hibernate.exception.SQLGrammarException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) LockAcquisitionException(org.hibernate.exception.LockAcquisitionException) PessimisticLockException(org.hibernate.PessimisticLockException)

Example 2 with SQLGrammarException

use of org.hibernate.exception.SQLGrammarException in project hibernate-orm by hibernate.

the class TestConnectionPool method testConnectionPoolDoesNotConsumeAllConnections.

@Test
public void testConnectionPoolDoesNotConsumeAllConnections() {
    for (int i = 0; i < CONNECTION_POOL_SIZE + 1; ++i) {
        EntityManager entityManager = getOrCreateEntityManager();
        try {
            for (int j = 0; j < 2; j++) {
                try {
                    final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
                    final CriteriaQuery<TestEntity> criteriaQuery = builder.createQuery(TestEntity.class);
                    criteriaQuery.select(criteriaQuery.from(TestEntity.class));
                    entityManager.createQuery(criteriaQuery).getResultList();
                } catch (PersistenceException e) {
                    if (e.getCause() instanceof SQLGrammarException) {
                    //expected, the schema was not created
                    } else {
                        throw e;
                    }
                }
            }
        } finally {
            entityManager.close();
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) SQLGrammarException(org.hibernate.exception.SQLGrammarException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Example 3 with SQLGrammarException

use of org.hibernate.exception.SQLGrammarException in project hibernate-orm by hibernate.

the class SubclassTest method testDefault.

@Test
public void testDefault() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    File doc = new Document("Enron Stuff To Shred", 1000);
    Folder folder = new Folder("Enron");
    s.persist(doc);
    s.persist(folder);
    try {
        tx.commit();
    } catch (SQLGrammarException e) {
        System.err.println(e.getSQLException().getNextException());
    }
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    List result = s.createCriteria(File.class).list();
    assertNotNull(result);
    assertEquals(2, result.size());
    File f2 = (File) result.get(0);
    checkClassType(f2, doc, folder);
    f2 = (File) result.get(1);
    checkClassType(f2, doc, folder);
    s.delete(result.get(0));
    s.delete(result.get(1));
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) SQLGrammarException(org.hibernate.exception.SQLGrammarException) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with SQLGrammarException

use of org.hibernate.exception.SQLGrammarException in project hibernate-orm by hibernate.

the class CompositeIdTest method testDistinctCountOfEntityWithCompositeId.

@Test
@SkipForDialect(value = Oracle8iDialect.class, comment = "Cannot count distinct over multiple columns in Oracle")
public void testDistinctCountOfEntityWithCompositeId() {
    // today we do not account for Dialects supportsTupleDistinctCounts() is false.  though really the only
    // "option" there is to throw an error.
    final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan("select count(distinct o) from Order o", false, Collections.EMPTY_MAP);
    assertEquals(1, plan.getTranslators().length);
    final QueryTranslator translator = plan.getTranslators()[0];
    final String generatedSql = translator.getSQLString();
    System.out.println("Generated SQL : " + generatedSql);
    final int countExpressionListStart = generatedSql.indexOf("count(");
    final int countExpressionListEnd = generatedSql.indexOf(")", countExpressionListStart);
    final String countExpressionFragment = generatedSql.substring(countExpressionListStart + 6, countExpressionListEnd + 1);
    assertTrue(countExpressionFragment.startsWith("distinct"));
    assertTrue(countExpressionFragment.contains(","));
    Session s = openSession();
    s.beginTransaction();
    Customer c = new Customer();
    c.setCustomerId("1");
    c.setAddress("123 somewhere");
    c.setName("Brett");
    Order o1 = new Order(c);
    o1.setOrderDate(Calendar.getInstance());
    Order o2 = new Order(c);
    o2.setOrderDate(Calendar.getInstance());
    s.persist(c);
    s.persist(o1);
    s.persist(o2);
    s.getTransaction().commit();
    s.clear();
    s.beginTransaction();
    try {
        long count = (Long) s.createQuery("select count(distinct o) FROM Order o").uniqueResult();
        if (!getDialect().supportsTupleDistinctCounts()) {
            fail("expected SQLGrammarException");
        }
        assertEquals(2l, count);
    } catch (SQLGrammarException e) {
        if (getDialect().supportsTupleDistinctCounts()) {
            throw e;
        }
    }
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.createQuery("delete from Order").executeUpdate();
    s.createQuery("delete from Customer").executeUpdate();
    s.getTransaction().commit();
    s.close();
}
Also used : HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) SQLGrammarException(org.hibernate.exception.SQLGrammarException) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 5 with SQLGrammarException

use of org.hibernate.exception.SQLGrammarException in project hibernate-orm by hibernate.

the class CriteriaHQLAlignmentTest method testCountReturnValues.

@Test
@SkipForDialect(value = Oracle8iDialect.class, comment = "Cannot count distinct over multiple columns in Oracle")
public void testCountReturnValues() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Human human1 = new Human();
    human1.setName(new Name("John", 'Q', "Public"));
    human1.setNickName("Johnny");
    s.save(human1);
    Human human2 = new Human();
    human2.setName(new Name("John", 'A', "Doe"));
    human2.setNickName("Johnny");
    s.save(human2);
    Human human3 = new Human();
    human3.setName(new Name("John", 'A', "Doe"));
    human3.setNickName("Jack");
    s.save(human3);
    Human human4 = new Human();
    human4.setName(new Name("John", 'A', "Doe"));
    s.save(human4);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Long count = (Long) s.createQuery("select count( * ) from Human").uniqueResult();
    assertEquals(4, count.longValue());
    s.clear();
    count = (Long) s.createCriteria(Human.class).setProjection(Projections.rowCount()).uniqueResult();
    assertEquals(4, count.longValue());
    s.clear();
    count = (Long) s.createQuery("select count( nickName ) from Human").uniqueResult();
    assertEquals(3, count.longValue());
    s.clear();
    count = (Long) s.createCriteria(Human.class).setProjection(Projections.count("nickName")).uniqueResult();
    assertEquals(3, count.longValue());
    s.clear();
    count = (Long) s.createQuery("select count( distinct nickName ) from Human").uniqueResult();
    assertEquals(2, count.longValue());
    s.clear();
    count = (Long) s.createCriteria(Human.class).setProjection(Projections.count("nickName").setDistinct()).uniqueResult();
    assertEquals(2, count.longValue());
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    try {
        count = (Long) s.createQuery("select count( distinct name ) from Human").uniqueResult();
        if (!getDialect().supportsTupleDistinctCounts()) {
            fail("expected SQLGrammarException");
        }
        assertEquals(2, count.longValue());
    } catch (SQLGrammarException ex) {
        if (!getDialect().supportsTupleDistinctCounts()) {
        // expected
        } else {
            throw ex;
        }
    } finally {
        t.rollback();
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    try {
        count = (Long) s.createCriteria(Human.class).setProjection(Projections.count("name").setDistinct()).uniqueResult();
        if (!getDialect().supportsTupleDistinctCounts()) {
            fail("expected SQLGrammarException");
        }
        assertEquals(2, count.longValue());
    } catch (SQLGrammarException ex) {
        if (!getDialect().supportsTupleDistinctCounts()) {
        // expected
        } else {
            throw ex;
        }
    } finally {
        t.rollback();
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    count = (Long) s.createQuery("select count( distinct name.first ) from Human").uniqueResult();
    assertEquals(1, count.longValue());
    s.clear();
    count = (Long) s.createCriteria(Human.class).setProjection(Projections.count("name.first").setDistinct()).uniqueResult();
    assertEquals(1, count.longValue());
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    try {
        count = (Long) s.createQuery("select count( name ) from Human").uniqueResult();
        if (!getDialect().supportsTupleCounts()) {
            fail("expected SQLGrammarException");
        }
        assertEquals(1, count.longValue());
    } catch (SQLGrammarException ex) {
        if (!getDialect().supportsTupleCounts()) {
        // expected
        } else {
            throw ex;
        }
    } catch (PersistenceException e) {
        SQLGrammarException cause = assertTyping(SQLGrammarException.class, e.getCause());
        if (!getDialect().supportsTupleCounts()) {
        // expected
        } else {
            throw e;
        }
    } finally {
        t.rollback();
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    try {
        count = (Long) s.createCriteria(Human.class).setProjection(Projections.count("name")).uniqueResult();
        if (!getDialect().supportsTupleCounts()) {
            fail("expected SQLGrammarException");
        }
        assertEquals(1, count.longValue());
    } catch (SQLGrammarException ex) {
        if (!getDialect().supportsTupleCounts()) {
        // expected
        } else {
            throw ex;
        }
    } finally {
        t.rollback();
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    s.createQuery("delete from Human").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) SQLGrammarException(org.hibernate.exception.SQLGrammarException) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Aggregations

SQLGrammarException (org.hibernate.exception.SQLGrammarException)10 Test (org.junit.Test)8 Session (org.hibernate.Session)7 Transaction (org.hibernate.Transaction)5 PersistenceException (javax.persistence.PersistenceException)3 List (java.util.List)2 SkipForDialect (org.hibernate.testing.SkipForDialect)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 EntityManager (javax.persistence.EntityManager)1 Tuple (javax.persistence.Tuple)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 PessimisticLockException (org.hibernate.PessimisticLockException)1 QueryTimeoutException (org.hibernate.QueryTimeoutException)1 StaleObjectStateException (org.hibernate.StaleObjectStateException)1 SQLServerDialect (org.hibernate.dialect.SQLServerDialect)1 HQLQueryPlan (org.hibernate.engine.query.spi.HQLQueryPlan)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1