Frank
Frank

Reputation: 1027

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. 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?

Upvotes: 0

Views: 3559

Answers (2)

Alexander Golub
Alexander Golub

Reputation: 51

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

Upvotes: 0

Related Questions