Skip Headers

# UpdateAll Queries

Previous

Next

Use an UpdateAllQuery to update a large number of objects at once. With this query, you can update a large number of objects with a single SQL statement, instead of reading the objects into memory and updating them individually. Example: Using UpdateAllQuery shows an `UpdateAllQuery` to give all full-time employees a raise.

Using UpdateAllQuery

    // Give all full time employees a 10% raise
    UpdateAllQuery updateQuery = new UpdateAllQuery(Employee.class);
    ExpressionBuilder employee = updateQuery.getExpressionBuilder();
    updateQuery.setSelectionCriteria(eb.get("status").equal("FULL_TIME"));
    updateQuery.addUpdateExpression(employee.get("salary"),
        ExpressionMath.multiply(employee.get("salary"), new Float(1.10)));

Since multiple tables cannot be updated from the same SQL statement, the `UpdateAllQuery` does not support objects that span multiple tables, or inheritance. Additionally, the `UpdateAllQuery` must be executed from its own transaction–the unit of work must contain only the query. Use the `UnitOfWork` method `executeQuery`.

In a non-JTA transaction, TopLink provides support for the unit of work and session execute; in a JTA transaction, only the unit of work is supported–there is no support for the session execute. For more information on transactions, see Understanding TopLink Transactions.

UpdateAllQuery takes the cache into consideration and ensures that the cache is kept up to date. You can configure the UpdateAllQuery to invalidate cache (see "Cache Invalidation") by setting the cache usage to `INVALIDATE_CACHE` (default), or to not use the cache by specifying `NO_CACHE` option. You can manipulate these settings through the `setCacheUsage` method. You can only update the cache for expressions that can conform. For more information on cache, see Understanding the Cache.

UpdateAll queries only support unidirectional one-to-one relationships; there is a full support for direct, and a partial support for aggregate mappings (see Mappings).

Note: You can set an attribute within an aggregate only, but not an entire aggregate.

These queries do not support foreign key updates, therefore you cannot use these queries to set foreign key fields to `null`.

You can use an UpdateAll query with optimistic locking (see "Understanding Descriptors and Locking") at the level of updating a row in a database–there should be no updates in the cache. we will update the locking field on the database. There is also support for version and timestamp locking, as well as indirect support for field locking.