UPDATE SET Queries in JPA/JPQL

You can update existing entities, as explained in chapter 2, by following these steps:

  • Retrieve the entities into an EntityManager.
  • Update the relevant entity fields within an active transaction.
  • Apply the changes to the database by calling the commit method.

JPQL UPDATE queries provide an alternative way to update entities. Unlike SELECT queries, which retrieve data, UPDATE queries modify specified entities directly in the database without retrieving them first.

Using an UPDATE query can be more efficient than the retrieve-and-update approach, but you should use this method with caution because bypassing the EntityManager can disrupt its synchronization with the database. For example, the EntityManager might not be aware that an UPDATE query has modified a cached entity in its persistence context. Therefore, we recommend using a separate EntityManager for UPDATE queries.

As with any database modification, you must execute UPDATE queries within an active transaction. The changes become visible to other EntityManager instances only after the transaction is committed.

This page covers the following topics:

Update lll queriesSelective update

Update lll queries

The simplest form of an UPDATE query modifies all instances of a specified entity class in the database, including instances of its subclasses.

For example, the following three equivalent queries increase the population of all countries by 10%:

UPDATE Country SET population = population * 11 / 10
UPDATE Country c SET c.population = c.population * 11 / 10
UPDATE Country AS c SET c.population = c.population * 11 / 10

The UPDATE clause defines exactly one range variable (with or without an explicit variable name) for iteration. Multiple variables and JOIN operations are not supported. The SET clause defines one or more field-update expressions, which use the range variable if one is defined.

You can update multiple fields by providing a comma-separated list of expressions. For example:

UPDATE Country SET population = 0, area = 0

Execute UPDATE queries by using the executeUpdate()jakarta.persistence.Query.executeUpdate()Execute an update or delete statement. method:

  Query query = em.createQueryjakarta.persistence.EntityManager.createQuery(String)Create an instance of   Query   for executing a Jakarta Persistence query language statement.(
      "UPDATE Country SET population = 0, area = 0");
  int updateCount = em.executeUpdatejakarta.persistence.Query.executeUpdate()Execute an update or delete statement.();

If no transaction is active, a TransactionRequiredExceptionjakarta.persistence.TransactionRequiredExceptionThrown by the persistence provider when a transaction is required but is not active. is thrown.

On success, the executeUpdate() method returns the number of objects that the query modified.

Selective update

UPDATE queries support the WHERE clause, which is essential for updating specific entities. However, they do not support the GROUP BY, HAVING, and ORDER BY clauses.

For example, the following query updates the population of countries whose population is less than a specified limit:

UPDATE Country
SET population = population * 11 / 10
WHERE population < :p

You can execute the query as follows:

  Query query = em.createQueryjakarta.persistence.EntityManager.createQuery(String)Create an instance of   Query   for executing a Jakarta Persistence query language statement.(
      "UPDATE Country SET population = population * 11 / 10 " +
      "WHERE population < :p");
  int updateCount = query.setParameterjakarta.persistence.Query.setParameter(String,Object)Bind an argument value to a named parameter.(p, 100000).executeUpdatejakarta.persistence.Query.executeUpdate()Execute an update or delete statement.();