The only CriteriaQuery examples I've seen are for SELECT queries. Is there a way to construct typesafe DELETE queries, either with JPA 2 or Hibernate APIs?
Asked
Active
Viewed 6,083 times
4 Answers
7
I don't think it's possible.
Quoted from http://blogs.oracle.com/ldemichiel/entry/java_persistence_2_0_public1 :
The Criteria API does not currently support update and delete operations

Cojones
- 1,906
- 22
- 25
5
It is not directly possible, but you can use typesafe DELETE and UPDATE queries via the Querydsl JPA extension. Querydsl uses JPQL internally, but provides a typesafe fluent interface for the query construction.
I am the maintainer of Querydsl, so this answer is biased.

Timo Westkämper
- 21,824
- 5
- 78
- 111
-6
not sure if you can do it with Criteria but it is possible to create HQL with bulk operations such as delete or update. Check this example from "Persistence with Hibernate" book (page 535):
Query q = session.createQuery(
"delete CreditCard c where c.stolenOn is not null"
);
int updatedCreditCards = q.executeUpdate();
Best regards!

Lucas de Oliveira
- 1,642
- 11
- 16