Java Reference
In-Depth Information
Note
if an application utilizes FUnCtiOn invocation, it may not be portable across databases.
Bulk Updates and Deletions
The Criteria API has been enhanced to support bulk updates and deletions. The Criteria API allows developers to
utilize Java language syntax in order to perform database queries and manipulations, rather than JPQL or SQL.
A javax.persistence.criteria.CriteriaUpdate object can be used to perform bulk update operations, and a
javax.persistence.critera.CriteriaDelete object can be used to perform bulk deletion operations. How do
we obtain such objects? The Criteria API is dependent upon the javax.persistence.criteria.CriteriaBuilder
interface, which is used to return objects that can be used to work with specified Entity classes. In the JPA 2.1 release,
the CriteriaBuilder has been updated to include the methods createCriteriaUpdate and createCriteriaDelete ,
which will return the CriteriaUpdate or CriteriaDelete objects, respectively.
To make use of the CriteriaBuilder , you first need to obtain a CriteriaBuilder from the EntityManager . You
can then use the CriteriaBuilder to obtain the CriteriaUpdate or CriteriaDelete object of your choosing. In the
following lines of code, a CriteriaUpdate object is obtained for use with an Employee entity.
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaUpdate<Employee> q = builder.createCriteriaUpdate(Employee.class);
Once obtained, the CriteriaUpdate can be used to build a query and set values, as desired, for making the
necessary updates or deletions. In the following excerpt, the CriteriaUpdate object is used to update all Employee
objects that have a status of INACTIVE , changing that status to ACTIVE .
Root<Employee> e = q.from(Employee.class);
q.set(e.get("status"), "ACTIVE")
.where(builder.equal(e.get("status"), "INACTIVE"));
Let's break this down a bit to explain what exactly is going on. First, the query root is set by calling the q.from
method and passing the entity class for which we wish to obtain the root, where q is the CriteriaUpdate object. Next,
the q.set method is invoked, passing the Path to the Employee status attribute, along with the ACTIVE String. The
q.set method is performing the bulk update. To further refine the query, a WHERE clause is added by adding a chained
call to the .where method, and passing the Employee objects that have a status of INACTIVE . To see an entire example,
please visit the org.javaee7.chapter04.jsf.CriteriaUpdates class. The entire Criteria update looks as follows:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaUpdate<Employee> q = builder.createCriteriaUpdate(Employee.class);
Root<Employee> e = q.from(Employee.class);
q.set(e.get("status"), "ACTIVE")
.where(builder.equal(e.get("status"), "INACTIVE"));
Finally, to complete the transaction, you must create the Query object and then execute it using the following
lines of code:
Query criteriaUpd = em.createQuery(q);
criteriaUpd.executeUpdate();
The complete code for the bulk update in this example resides within a method named
updateEmployeeStatusInactive , which is listed below.
 
 
Search WWH ::




Custom Search