2

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?

4 Answers 4

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

0
7

It will be available in JPA 2.1 version.

1
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.

-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!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.