org.hibernate.hql.internal.ast.QuerySyntaxException:意外标记

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token

当我尝试实施下面的 query 时出现以下错误:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token 

我的查询在扩展 CrudRepository

的 Spring 数据 PersonRepository

查询:

@Modifying
@Transactional
@Query("DELETE (entity) FROM Person entity WHERE entity.id = :id")
List<Person> deleteFromPersonWithId(@Param("id") String id);

我的语法有什么错误?

您没有正确的 DELETE 查询语法,它应该看起来像这样。

DELETE FROM Person entity WHERE entity.id = :id

顺便说一下,有一个 delete 方法可以在 CrudRepository 本身中完全满足您的需求。所以不需要复制它。

查询必须是:

@Query("DELETE FROM Person entity WHERE entity.id = :id")
entityManager.remove(entityInstance)

将在提交事务时从数据库中删除实体。

这样更改您的注释;

@Modifying
@Transactional
@Query("DELETE FROM Person WHERE id = :id")
void deleteFromPersonWithId(@Param("id") String id);