1

I want to create SQL query for deleting rows. I tried this:

public void deleteByIds(List<Integer> ids)
{
        String hql = "delete from " + OnboardingTasks.class.getName() + " e WHERE e.id IN :ids";
        TypedQuery<OnboardingTasks> query = entityManager.createQuery(hql, OnboardingTasks.class).setParameter("ids", ids);
        query.executeUpdate();
}

But I get exception: ption [Request processing failed; nested exception is java.lang.IllegalArgumentException: Update/delete queries cannot be typed] with root cause java.lang.IllegalArgumentException: Update/delete queries cannot be typed Do you know what is the proper way to send a list of Id's to delete?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 1
    Does this answer your question? [Update/delete queries cannot be typed JPA](https://stackoverflow.com/questions/28439141/update-delete-queries-cannot-be-typed-jpa) – luk2302 Sep 13 '21 at 12:51

1 Answers1

1

A TypedQuery<X> is a query that yields a return object of the type X or a list thereof. Update and delete queries don't return anything, so you have to use a different type of query for them.

Using EntityManager.createQuery(String) instead of EntityManager.createQuery(String, Class) should do the trick.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348