java.lang.IllegalArgumentException: Update/delete queries cannot be typed

Issue

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?

Solution

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.

Answered By – Jens Schauder

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published