-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem when trying to truncate all tables
PostPosted: Wed Aug 19, 2009 2:09 pm 
Newbie

Joined: Wed Aug 19, 2009 1:54 pm
Posts: 1
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 &apos;TABLE_NAME&apos; 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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.