You can delete all instances of a class, one at a time, using this method. However, if you have many records, this is slower, you do not duplicate the literal string for the table name.
public static void removeAllInstances(final Class<?> clazz) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); final List<?> instances = session.createCriteria(clazz).list(); for (Object obj : instances) { session.delete(obj); } session.getTransaction().commit(); }
using:
removeAllInstances(User_Role.class);
source share