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...)
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.