Hi, I'm very new to Hibernate and I'm having problems trying to get the following code to work:
Code:
public class DatabaseSteps extends AbstractSeleniumSteps {
public static final String FOREIGN_KEY_DISABLE = "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;";
public static final String FOREIGN_KEY_ENABLE = "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;";
...
public void initialiseDatabase() throws Exception {
Transaction trans = session.beginTransaction();
List<String> allTables = session.createSQLQuery("SHOW TABLES").list();
for (String table : allTables) {
session.createSQLQuery(FOREIGN_KEY_DISABLE + " TRUNCATE TABLE " + table + "; " + FOREIGN_KEY_ENABLE).executeUpdate();
}
trans.commit();
}
It's basically trying to get a list of all the tables and then truncating each one in turn. This is because it's a key part of some JBehave tests which require the database to be cleared out before each scenario.
Previously this code used a simple prepared statement, but I was trying to get it to use Hibernate for consistency's sake (the Hibernate cfg file points at a particular URL that I can then manipulate in /etc/hosts to point at various test servers).
I've Googled this extensively and haven't found a clear answer to why the above code simply generates the following errors:
Quote:
org.
hibernate.exception.SQLGrammarException: could not execute query
and
Quote:
Caused by: java.sql.SQLException: Column 'TABLE_NAME' not found.
I've tried altering the code to eliminate the loop and just truncate a named table, but then I get the error:
Quote:
org.hibernate.exception.SQLGrammarException: could not execute native bulk mani
pulation query
I know I've committed some desperate newbie blunder here, but I can't figure it out through lack of experience. Can anyone help?