0

I'm using Hibernate in my project and using "Query" Like below.

Query query = em.createQuery("delete from User where name=:name");
query.setParameter("name", "Zigi");
int deleted = query.executeUpdate();

I'm getting the result. When i'm using generics Like below

Query<?> query = em.createQuery("delete from User where name=:name");
query.setParameter("name", "Zigi");
int deleted = query.executeUpdate();

getting the result because "?" is something as wildcard (or) it will accept any datatype

when i'm using the code as below getting some error( java.lang.IllegalArgumentException: Update/delete queries cannot be typed ). i'm using Integer datatype because createQuery return number when it's executed

Query<Integer> query = em.createQuery("delete from User where name=:name", Integer.class);
query.setParameter("name", "Zigi");
int deleted = query.executeUpdate();

Any suggestions. I have to use Generics with specific datatype in it like above code.

thanks in advance

2
  • What error are you getting? Commented Nov 13, 2018 at 16:40
  • java.lang.IllegalArgumentException: Update/delete queries cannot be typed Commented Nov 14, 2018 at 4:28

1 Answer 1

0

Unfortunately executeUpdate does not allow you to use a typed query.

Instead you can use uniqueResult, and cast to the type that you want:

Query query = em.createQuery("delete from User where name=:name");
query.setParameter("name", "Zigi");
int deleted = (Integer) query.uniqueResult();

Referencing this answer

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.