简体   繁体   English

如何在休眠状态下编写HQL查询以删除所有行而不删除表?

[英]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? 如何在休眠状态下编写HQL查询以删除所有行而不删除表?
and
How to delete all table in database using HQL query? 如何使用HQL查询删除数据库中的所有表?

Assuming that your table is called your_table which corresponds to the Entity class Your_Table , the following should work: 假设您的表名为your_table ,它对应于EntityYour_Table ,则以下内容应该起作用:

@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. 如果要删除数据库中的所有表,则必须使用原始SQL命令。 HQL was not intended for creating/deleting tables, but rather for manipulating Entity objects created from tables. HQL并非旨在创建/删除表,而是用于处理从表创建的Entity对象。 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: 如果您确实要删除数据库中的所有表,则可以删除数据库本身,然后使用原始SQL重新创建它:

DROP DATABASE your_database;
CREATE DATABASE your_database;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM