Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using hibernate/hql to truncate a table?

Tags:

What is the recommended way to truncate a table using hibernate/hql?

I've tried this:

 Query query = session.createQuery("truncate table MyTable");
 query.executeUpdate();

But it didn't work (truncate doesn't seem do be documented anywhere in hql...)

like image 818
user149100 Avatar asked Aug 11 '09 20:08

user149100


1 Answers

You can use session.createSQLQuery() instead:

session.createSQLQuery("truncate table MyTable").executeUpdate();

Needless to say, this is not ideal in terms of portability. It's probably a good idea to define this query in mapping and retrieve it in code as named query.

like image 181
ChssPly76 Avatar answered Sep 21 '22 19:09

ChssPly76