hibernate and delete all

feiroox picture feiroox · Aug 16, 2010 · Viewed 56k times · Source

What is the best way to delete all rows in a table in Hibernate?

If I iterate over a collection and call session.delete() it's not performing to my knowledge.

If I use another option session.createQuery("delete ...") it doesn't affect persistence context.

When I should use these methods if there is no better variant?

Answer

Damian Leszczyński - Vash picture Damian Leszczyński - Vash · Aug 16, 2010

You can use HQL for truncate table

public int hqlTruncate(String myTable){
    String hql = String.format("delete from %s",myTable);
    Query query = session.createQuery(hql)
    return query.executeUpdate();
}