hertzsprung
hertzsprung

Reputation: 9893

JPA/Hibernate typesafe DELETE queries

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

Answers (4)

sinuhepop
sinuhepop

Reputation: 20316

It will be available in JPA 2.1 version.

Upvotes: 7

Timo Westkämper
Timo Westkämper

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

Cojones
Cojones

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

Lucas de Oliveira
Lucas de Oliveira

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

Related Questions