简体   繁体   English

为什么我不能在 spring 引导中使用“截断”

[英]why can't I use “truncate” in spring boot

I wanna use "truncate table" statement instead of "delete" statement in spring boot project cause I need reset the auto increment id in mysql.我想在 spring 引导项目中使用“截断表”语句而不是“删除”语句,因为我需要在 mysql 中重置自动增量 ID。 Here is my code:这是我的代码:

@PersistenceContext protected EntityManager em;
@Override
  public void removeAllShopeeCategory() {
    StringBuilder query = new StringBuilder("truncate table ShopeeCategoryDto shopeecategory");
    Query q = this.em.createQuery(query.toString());
    q.executeUpdate();
  }

but there is an exception like this:但有一个例外是这样的:

nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: truncate near line 1, column 1

other operation has worked, such as insert, update or select, what's the reason and what should I modify it?其他操作,如插入、更新或select,是什么原因,我应该修改什么?

 entityManager.createNativeQuery("TRUNCATE TABLE " + tableName + " CASCADE")
            .executeUpdate()

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

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