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

时间:2019-10-10 06:18:28

标签: spring hibernate spring-boot spring-data-jpa

我想在Spring Boot项目中使用“ truncate table”语句而不是“ delete”语句,因为我需要在mysql中重置自动增量ID。这是我的代码:

{...hen concat('_',$out) else '...}: Unexpected token name "_" beyond end of expression

但有一个例外:

@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();
  }

其他操作奏效,例如插入,更新或选择,这是什么原因,我应该对其进行修改吗?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

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