Chapter 6. Batch processing

download PDF
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.

6.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 EJB-QL (Chapter 7, EJB-QL: The Object Query Language).
The pseudo-syntax for UPDATE and DELETE statements is: ( UPDATE | DELETE ) FROM? ClassName (WHERE WHERE_CONDITIONS)?. Note that:
  • In the from-clause, the FROM keyword is optional.
  • There can only be a single class named in the from-clause, and it cannot have an alias (this is a current Hibernate limitation and will be removed soon).
  • No joins (either implicit or explicit) can be specified in a bulk EJB-QL query. Sub-queries may be used in the where-clause.
  • The where-clause is also optional.
As an example, to execute an EJB-QL UPDATE, use the Query.executeUpdate() method:
EntityManager entityManager = entityManagerFactory.createEntityManager();

String ejbqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = entityManager.createQuery( ejbqlUpdate )
                            .setParameter( "newName", newName )
                            .setParameter( "oldName", oldName )
To execute an EJB-QL DELETE, use the same Query.executeUpdate() method (the method is named for those familiar with JDBC's PreparedStatement.executeUpdate()):
EntityManager entityManager = entityManagerFactory.createEntityManager();

String hqlDelete = "delete Customer where name = :oldName";
int deletedEntities = entityManager.createQuery( hqlDelete )
                            .setParameter( "oldName", oldName )
The int value returned by the Query.executeUpdate() method indicate the number of entities affected by the operation. This may or may not correlate with the number of rows affected in the database. An EJB-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 inheritence hierarchy.
Red Hat logoGithubRedditYoutubeTwitter


Try, buy, & sell


About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.