Chapter 7. Batch processing

Batch processing has traditionally been difficult in full
object/relational mapping. ORM is all about object state management, which
implies that object state is available in memory. However, Hibernate has
some features to optimize batch processing which are discussed in the
Hibernate reference guide, however, EJB3 persistence differs
slightly.

7.1. Bulk update/delete

As already discussed, automatic and transparent object/relational
mapping is concerned with the management of object state. This implies
that the object state is available in memory, hence updating or deleting
(using SQL UPDATE and DELETE) data
directly in the database will not affect in-memory state. However,
Hibernate provides methods for bulk SQL-style UPDATE
and DELETE statement execution which are performed
through JP-QL (Chapter 8, JP-QL: The Object Query Language).

The int value returned by the
Query.executeUpdate() method indicate the number of
entities effected by the operation. This may or may not correlate with the
number of rows effected in the database. A JP-QL bulk operation might
result in multiple actual SQL statements being executed, for
joined-subclass, for example. The returned number indicates the number of
actual entities affected by the statement. Going back to the example of
joined-subclass, a delete against one of the subclasses may actually
result in deletes against not just the table to which that subclass is
mapped, but also the "root" table and potentially joined-subclass tables
further down the inheritance hierarchy.