Request to delete all rows in table hibernation

I am trying to delete all rows in a user_role table with a request for hibernation. But every time I get errors. Can someone please help me with this.

DaoImpl

@Override public void deleteAll() { session.getCurrentSession().delete(/*delete all query*/); } 

model class

 @Entity @Table(name="user_role") public class User_Role { @Id @Column @GeneratedValue(strategy=GenerationType.AUTO) private int id; @Column(name="role_name") private String name; //setter and getter } 
+6
source share
2 answers

try the following:

 sessionFactory.getCurrentSession().createQuery("delete from User_Role").executeUpdate(); 
+10
source

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); 
0
source

Source: https://habr.com/ru/post/973257/


All Articles