java123999
java123999

Reputation: 7394

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

I am getting the following error when I try to implement my query below:

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

My query is within a Spring data PersonRepository that extends CrudRepository

Query:

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

What is the error in my syntax?

Upvotes: 1

Views: 1788

Answers (4)

Marius
Marius

Reputation: 3141

Change your annotations as such;

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

Upvotes: 0

Bhushan
Bhushan

Reputation: 567

entityManager.remove(entityInstance)

will remove entity from DB when transaction is committed.

Upvotes: 0

Petr Mensik
Petr Mensik

Reputation: 27526

You don't have a right syntax of DELETE query, it should look like this.

DELETE FROM Person entity WHERE entity.id = :id

By the way there is a delete method which does exactly what you want in the CrudRepository itself. So no need to duplicate it.

Upvotes: 1

Jens
Jens

Reputation: 69470

Query must be:

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

Upvotes: 0

Related Questions