Question

is it possible to do a hibernate bulk delete using an inner query? Example:

DELETE Person person WHERE person.id IN (SELECT id FROM...)
Was it helpful?

Solution

Yes. It is possible. See below points.

Hibernate provides methods for bulk SQL-style DML statement execution that is performed through the Hibernate Query Language (HQL).

The pseudo-syntax for UPDATE and DELETE statements is:

( UPDATE | DELETE ) FROM? EntityName (WHERE where_conditions)

Some points to note:

  1. In the from-clause, the FROM keyword is optional
  2. There can only be a single entity named in the from-clause. It can, however, be aliased. If the entity name is aliased, then any property references must be qualified using that alias. If the entity name is not aliased, then it is illegal for any property references to be qualified.
  3. No joins, either implicit or explicit, can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.
  4. The where-clause is also optional.

References

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top