Reputation: 1027
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
Reputation: 51
entityManager.createNativeQuery("TRUNCATE TABLE " + tableName + " CASCADE")
.executeUpdate()
Upvotes: 0
Reputation: 1397
Please use https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery-java.lang.String- with native sql queries.
Upvotes: 1