Reputation: 9893
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?
Upvotes: 2
Views: 6104
Reputation: 22190
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.
Upvotes: 5
Reputation: 1954
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
Upvotes: 7
Reputation: 1632
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!
Upvotes: -6