Book Image

EJB 3 Developer Guide

By : Michael Sikora
Book Image

EJB 3 Developer Guide

By: Michael Sikora

Overview of this book

Table of Contents (18 chapters)
EJB 3 Developer Guide
Credits
About the Author
About the Reviewers
Preface
Annotations and Their Corresponding Packages

Bulk Update and Delete


Bulk update and delete operations allow you to delete or update a number of entity instances of a single entity class including its subclasses. For example, the statement

DELETE FROM Account a WHERE a.customer IS NULL

will remove from the database all account entities that do not have an associated customer. A delete operation does not cascade to related entities, such as Customer in our example. For bulk update and delete operations we use the Query.executeUpdate() method to execute the query. So we can wrap the above query in a deleteOrphanedAccounts() method, say, as follows:

public void deleteOrphanedAccounts() {
em.createQuery("DELETE FROM Account a WHERE " +
"a.customer IS NULL").executeUpdate();
}

The bulk update syntax is similar to that in SQL. For example, the query

UPDATE Account a SET a.balance = a.balance * 1.1
WHERE a.accountType = 'C'

will increase all checking account balances by 10 percent.

Bulk update and delete operations apply directly on the database...