Java Reference
In-Depth Information
Support for bulk update/delete
The bulk update and delete in the Criteria API are respectively constructed with
javax.persistence.criteria.CriteriaUpdate
and
javax.persistence.criteria.CriteriaDelete
interfaces. The following
code demonstrates how to update a lot of information with just one Criteria API re-
quest:
//bulk update
CriteriaUpdate cUpdate =
cb.createCriteriaUpdate(Student.class);
Root root = cUpdate.from(Student.class);
cUpdate.set(root.get("departId"),
"GT").where(cb.equal(root.get("departId"),
"GI"));
Query q = em.createQuery(cUpdate);
em.getTransaction().begin();//begin transaction
int num = q.executeUpdate();
em.getTransaction().commit();//commit
transaction
System.out.println("number of update : "+num);
//JPQL equivalent query
UPDATE Student a SET a.departId = 'GT' WHERE
a.departId = 'GI'
Support for new reserved identifiers
Just like the JPQL, the Criteria API incorporates the possibility of making downcasts
and defines joins using
ON
conditions. To do that, overloaded
treat()
methods
have been added to the
javax.persistence.criteria.CriteriaBuilder
interface for downcasting, while
on()
and
getOn()
methods have been added
to some interfaces (such as
Join
,
ListJoin
,
SetJoin
,
MapJoin
,
Collec-
tionJoin
,and
Fetch
) of the
javax.persistence.criteria
package for joins
with
ON
conditions. The following query is equivalent to the JPQL downcasting
shown in the preceding code: