Update of multiple records in a batch is called bulk update. To perform bulk update, you use the Bulk UPDATE statement and Bulk DELETE statement. The bulk UPDATE and DELETE operations are applied to a stand-alone entity class or an entity class matching with the subclass. With the UPDATE statement, you define the identification variable in the UPDATE clause, and with the DELETE statement, you define the identification variable in the FROM clause and specify one type of entity.
Notes on using the Bulk UPDATE statement and Bulk DELETE statement:
- The delete operation is only applied to the entities of the specified class and the subclasses. The related entities are not cascaded.
- The update value specified in the update operation (new_value) must be compatible with the allocated field type. If the update value is not compatible, an exception occurs.
- The Bulk UPDATE statement executes the database update operation without setting the optimistic lock, so the processing related to the optimistic lock is not executed. Therefore, the value in the version column must be referenced and updated manually.
- Note
- When you execute the Bulk UPDATE statement and the Bulk DELETE statement, note that mismatch occurs between the database and the entities of the activated persistence context. The operations in the Bulk UPDATE statement and the Bulk DELETE statement must be executed when separated from the transaction or when the transaction is started.
An example of coding the Bulk UPDATE statement and Bulk DELETE statement is as follows:
UPDATE EMPLOYEE
SET MONTHLY_SALARY = MONTHLY_SALARY + 1000
WHERE DEPARTMENT_ID = 3
DELETE FROM EMPLOYEE e
WHERE e.EMPLOYEE_NAME IS NULL AND e.monthlySalary IS EMPTY |
For details on the syntax of the Bulk UPDATE statement and Bulk DELETE statement, see Appendix D BNF for JPQL.