Query to delete all rows in a table hibernate

Sanjay Kumar picture Sanjay Kumar · Aug 2, 2014 · Viewed 23.1k times · Source

I am trying to delete all rows in table 'user_role' with hibernate query. But every time i am getting errors. Can someone please help me with it.

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 
}

Answer

prashant thakre picture prashant thakre · Aug 2, 2014

try this:

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