vishal
vishal

Reputation: 33

How to write HQL query in hibernate to delete all rows without deleting table?

How to write HQL query in hibernate to delete all rows without deleting table?
and
How to delete all table in database using HQL query?

Upvotes: 1

Views: 3762

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

Assuming that your table is called your_table which corresponds to the Entity class Your_Table, the following should work:

@Entity @Table(name="your_table")
public class Your_Table {

    @Id @Column @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    // columns go here
    // getters and setters
}

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

If you want to drop all tables in your database, you will have to use raw SQL commands. HQL was not intended for creating/deleting tables, but rather for manipulating Entity objects created from tables. If you really want to drop all tables in your database, you can just drop the database itself and then re-create it using raw SQL:

DROP DATABASE your_database;
CREATE DATABASE your_database;

Upvotes: 1

Related Questions