Java Reference
In-Depth Information
Using UPDATE
Only one entity type can be specified with an
UPDATE
statement, and you should provide
a
WHERE
clause to limit the number of entities affected by the statement. Here's the syntax
for the
UPDATE
statement:
UPDATE entityName indentifierVariable
SET single_value_path_expression1 = value1, ...
single_value_path_expressionN = valueN
WHERE where_clause
You can use any persistence field and single-value association field in the
SET
clause of
the
UPDATE
statement. Assume that you want to provide gold status and a commission rate
of 10% to all sellers whose last name starts with
Packrat
. Start with the following JPQL
statement:
UPDATE Seller s
SET s.status = 'G', s.commissionRate = 10
WHERE s.lastName like 'Packrat%'
It's clear from this statement that the
WHERE
clause of an
UPDATE
behaves exactly the
same as the one you used in the
SELECT
statement. We'll return to a detailed discussion
of the
WHERE
clause later in this chapter.
Using DELETE
Like
UPDATE
,
DELETE
in JPQL resembles its SQL cousin. You can specify only one en-
tity type with a
DELETE
statement, and you should specify a
WHERE
clause to limit the
number of entities affected by the statement. Here's the syntax for the
DELETE
statement:
DELETE entityName indentifierVariable
WHERE where_clause
For example, if you want to remove all instances of sellers with silver status, you'll use
this:
DELETE Seller s
WHERE s.status = 'Silver'