[英]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,是什么原因,我应该修改什么?
Please use https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery-java.lang.String- with native sql queries.请使用https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery-java.lang.String-与本机 ZAC5C74B64B4B8352AF2F181FBAC 查询。
entityManager.createNativeQuery("TRUNCATE TABLE " + tableName + " CASCADE")
.executeUpdate()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.