org.hibernate.exception.SQLGrammarException Java Examples

The following examples show how to use org.hibernate.exception.SQLGrammarException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GemFireXDDialect.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
        return new SQLExceptionConversionDelegate() {
                @Override
                public JDBCException convert(SQLException sqlException,
                                String message, String sql) {
                        final String sqlState = JdbcExceptionHelper
                                        .extractSqlState(sqlException);
                        if (sqlState != null) {
                                if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
                                        return new SQLGrammarException(message, sqlException,
                                                        sql);
                                } else if (DATA_CATEGORIES.contains(sqlState)) {
                                        return new DataException(message, sqlException, sql);
                                } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
                                        return new LockAcquisitionException(message,
                                                        sqlException, sql);
                                }
                        }
                        return null;
                }
        };
}
 
Example #2
Source File: GemFireXDDialect.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
  return new SQLExceptionConverter() {
    @Override
    public JDBCException convert(SQLException sqlException, String message,
        String sql) {
      final String sqlState = JDBCExceptionHelper
          .extractSqlState(sqlException);
      if (sqlState != null) {
        if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
          return new SQLGrammarException(message, sqlException, sql);
        }
        else if (DATA_CATEGORIES.contains(sqlState)) {
          return new DataException(message, sqlException, sql);
        }
        else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
          return new LockAcquisitionException(message, sqlException, sql);
        }
      }
      return null;
    }
  };
}
 
Example #3
Source File: GemFireXDDialect.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
  return new SQLExceptionConverter() {
    @Override
    public JDBCException convert(SQLException sqlException, String message,
        String sql) {
      final String sqlState = JDBCExceptionHelper
          .extractSqlState(sqlException);
      if (sqlState != null) {
        if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
          return new SQLGrammarException(message, sqlException, sql);
        }
        else if (DATA_CATEGORIES.contains(sqlState)) {
          return new DataException(message, sqlException, sql);
        }
        else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
          return new LockAcquisitionException(message, sqlException, sql);
        }
      }
      return null;
    }
  };
}
 
Example #4
Source File: GemFireXDDialect.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
        return new SQLExceptionConverter() {
                @Override
                public JDBCException convert(SQLException sqlException,
                                String message, String sql) {
                        final String sqlState = JdbcExceptionHelper
                                        .extractSqlState(sqlException);
                        if (sqlState != null) {
                                if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
                                        return new SQLGrammarException(message, sqlException,
                                                        sql);
                                } else if (DATA_CATEGORIES.contains(sqlState)) {
                                        return new DataException(message, sqlException, sql);
                                } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
                                        return new LockAcquisitionException(message,
                                                        sqlException, sql);
                                }
                        }
                        return null;
                }
        };
}
 
Example #5
Source File: GemFireXDDialect.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
        return new SQLExceptionConversionDelegate() {
                @Override
                public JDBCException convert(SQLException sqlException,
                                String message, String sql) {
                        final String sqlState = JdbcExceptionHelper
                                        .extractSqlState(sqlException);
                        if (sqlState != null) {
                                if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
                                        return new SQLGrammarException(message, sqlException,
                                                        sql);
                                } else if (DATA_CATEGORIES.contains(sqlState)) {
                                        return new DataException(message, sqlException, sql);
                                } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
                                        return new LockAcquisitionException(message,
                                                        sqlException, sql);
                                }
                        }
                        return null;
                }
        };
}
 
Example #6
Source File: GemFireXDDialect.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
  return new SQLExceptionConverter() {
    @Override
    public JDBCException convert(SQLException sqlException, String message,
        String sql) {
      final String sqlState = JDBCExceptionHelper
          .extractSqlState(sqlException);
      if (sqlState != null) {
        if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
          return new SQLGrammarException(message, sqlException, sql);
        }
        else if (DATA_CATEGORIES.contains(sqlState)) {
          return new DataException(message, sqlException, sql);
        }
        else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
          return new LockAcquisitionException(message, sqlException, sql);
        }
      }
      return null;
    }
  };
}
 
Example #7
Source File: GemFireXDDialect.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
  return new SQLExceptionConverter() {
    @Override
    public JDBCException convert(SQLException sqlException, String message,
        String sql) {
      final String sqlState = JDBCExceptionHelper
          .extractSqlState(sqlException);
      if (sqlState != null) {
        if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
          return new SQLGrammarException(message, sqlException, sql);
        }
        else if (DATA_CATEGORIES.contains(sqlState)) {
          return new DataException(message, sqlException, sql);
        }
        else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
          return new LockAcquisitionException(message, sqlException, sql);
        }
      }
      return null;
    }
  };
}
 
Example #8
Source File: SpringPhysicalNamingStrategyPostgresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"PERSON\"");

    // Expected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #9
Source File: SpringPhysicalNamingStrategyH2IntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"person\"");

    // Unexpected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #10
Source File: QuotedLowerCaseNamingStrategyPostgresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"PERSON\"");

    // Expected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #11
Source File: UnquotedUpperCaseNamingStrategyH2IntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"person\"");

    // Expected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #12
Source File: UnquotedLowerCaseNamingStrategyH2IntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"person\"");

    // Unexpected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #13
Source File: QuotedLowerCaseNamingStrategyH2IntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenException(String tableName) {
    Query query = entityManager.createNativeQuery("select * from " + tableName);

    // Unexpected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #14
Source File: QuotedLowerCaseNamingStrategyH2IntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"PERSON\"");

    // Expected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #15
Source File: QuotedUpperCaseNamingStrategyH2IntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"person\"");

    // Expected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #16
Source File: HibernateExceptionUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMissingTable_whenEntitySaved_thenSQLGrammarException() {
    thrown.expect(isA(PersistenceException.class));
    thrown.expectCause(isA(SQLGrammarException.class));
    thrown
        .expectMessage("SQLGrammarException: could not prepare statement");

    Configuration cfg = getConfiguration();
    cfg.addAnnotatedClass(Product.class);

    SessionFactory sessionFactory = cfg.buildSessionFactory();
    Session session = null;
    Transaction transaction = null;
    try {

        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        Product product = new Product();
        product.setId(1);
        product.setName("Product 1");
        session.save(product);
        transaction.commit();
    } catch (Exception e) {
        rollbackTransactionQuietly(transaction);
        throw (e);
    } finally {
        closeSessionQuietly(session);
        closeSessionFactoryQuietly(sessionFactory);
    }
}
 
Example #17
Source File: HibernateExceptionUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMissingTable_whenQueryExecuted_thenSQLGrammarException() {
    thrown.expect(isA(PersistenceException.class));
    thrown.expectCause(isA(SQLGrammarException.class));
    thrown
        .expectMessage("SQLGrammarException: could not prepare statement");

    Session session = sessionFactory.openSession();
    NativeQuery<Product> query = session.createNativeQuery(
        "select * from NON_EXISTING_TABLE", Product.class);
    query.getResultList();
}
 
Example #18
Source File: FooStoredProceduresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
private boolean getFoosByNameExists() {
    try {
        Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(Foo.class);
        sqlQuery.list();
        return true;
    } catch (SQLGrammarException e) {
        LOGGER.error("WARNING : GetFoosByName() Procedure is may be missing ", e);
        return false;
    }
}
 
Example #19
Source File: FooStoredProceduresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
private boolean getAllFoosExists() {
    try {
        Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(Foo.class);
        sqlQuery.list();
        return true;
    } catch (SQLGrammarException e) {
        LOGGER.error("WARNING : GetAllFoos() Procedure is may be missing ", e);
        return false;
    }
}
 
Example #20
Source File: FooStoredProceduresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
private boolean getFoosByNameExists() {
    try {
        @SuppressWarnings("unchecked")
        NativeQuery<Foo> sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(Foo.class);
        sqlQuery.list();
        return true;
    } catch (SQLGrammarException e) {
        LOGGER.error("WARNING : GetFoosByName() Procedure is may be missing ", e);
        return false;
    }
}
 
Example #21
Source File: FooStoredProceduresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
private boolean getAllFoosExists() {
    try {
        @SuppressWarnings("unchecked")
        NativeQuery<Foo> sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(Foo.class);
        sqlQuery.list();
        return true;
    } catch (SQLGrammarException e) {
        LOGGER.error("WARNING : GetAllFoos() Procedure is may be missing ", e);
        return false;
    }
}
 
Example #22
Source File: UnquotedUpperCaseNamingStrategyPostgresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"PERSON\"");

    // Unexpected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #23
Source File: QuotedUpperCaseNamingStrategyPostgresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
    Query query = entityManager.createNativeQuery("select * from \"person\"");

    // Expected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #24
Source File: QuotedUpperCaseNamingStrategyPostgresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
    Query query = entityManager.createNativeQuery("select * from " + tableName);

    // Unexpected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #25
Source File: BatchChangeException.java    From we-cmdb with Apache License 2.0 5 votes vote down vote up
public static String extractExceptionMessage(Exception e) {
    if (e instanceof UndeclaredThrowableException) {
        Throwable cause = ((UndeclaredThrowableException) e).getUndeclaredThrowable().getCause();
        if (cause instanceof PersistenceException) {
            cause = ((PersistenceException) cause).getCause();
            if (cause instanceof DataException) {
                return ((DataException) cause).getSQLException().getMessage();
                
            }else if(cause instanceof SQLGrammarException) {
                return ((SQLGrammarException) cause).getSQLException().getMessage();
            }
        }
    }
    return e.getMessage();
}
 
Example #26
Source File: UnquotedLowerCaseNamingStrategyPostgresLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
    Query query = entityManager.createNativeQuery("select * from \"PERSON\"");

    // Expected result
    assertThrows(SQLGrammarException.class, query::getResultStream);
}
 
Example #27
Source File: RecentTagsContainer.java    From mamute with Apache License 2.0 5 votes vote down vote up
private void ignoreIfTableDidNotExist(SQLGrammarException ex) {
	if(ex.getCause().getMessage().contains(".question' doesn't exist")) {
		// ignore if its the first time we are running and the table still does not exist
		LOGGER.warn("Unable to run the mysql query to update the recent tags", ex);
	} else {
		// nasty catch and retrow, sorry.
		throw ex;
	}
}
 
Example #28
Source File: RecentTagsContainer.java    From mamute with Apache License 2.0 5 votes vote down vote up
public void execute() {
    // we need to do because of this class is app scoped
    Session session = sf.openSession();
    session.beginTransaction();
    try {
		update(session);
	} catch (SQLGrammarException ex) {
		ignoreIfTableDidNotExist(ex);
	}
    session.getTransaction().commit();
    session.close();
}
 
Example #29
Source File: SQLExceptionConversionTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testBadGrammar() throws Exception {
	SQLExceptionConverter converter = getDialect().buildSQLExceptionConverter();

	Session session = openSession();
	Connection connection = session.connection();

       // prepare/execute a query against a non-existent table
	PreparedStatement ps = null;
	try {
		ps = connection.prepareStatement("SELECT user_id, user_name FROM tbl_no_there");
		ps.executeQuery();

		fail("SQL compilation should have failed");
	}
	catch( SQLException sqle ) {
		assertEquals( "Bad conversion [" + sqle.getMessage() + "]", SQLGrammarException.class, converter.convert(sqle, null, null).getClass() );
	}
	finally {
		if ( ps != null ) {
			try {
				ps.close();
			}
			catch( Throwable ignore ) {
				// ignore...
			}
		}
	}

	session.close();
}
 
Example #30
Source File: GemFireXDDialect.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
        return new SQLExceptionConverter() {
                @Override
                public JDBCException convert(SQLException sqlException,
                                String message, String sql) {
                        final String sqlState = JdbcExceptionHelper
                                        .extractSqlState(sqlException);
                        if (sqlState != null) {
                                if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
                                        return new SQLGrammarException(message, sqlException,
                                                        sql);
                                } else if (DATA_CATEGORIES.contains(sqlState)) {
                                        return new DataException(message, sqlException, sql);
                                } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
                                        return new LockAcquisitionException(message,
                                                        sqlException, sql);
                                }
                        }
                        return null;
                }
        };
}