Close

JPQL UPDATE Statements

[Updated: Aug 8, 2018, Created: Jul 2, 2018]

JPQL UPDATE queries can be used to update entity objects in database. Following is the general syntax:

UPDATE entity_name [[AS] identification_variable] SET update_item {, update_item}*  

UPDATE queries can only be executed in a transaction and the changes are only visible to other users after commit.

Also Update and delete operations (update and delete “queries�) can only be executed using the Query#executeUpdate() method:

int executeUpdate()

For select queries we use Query#getResultList() and Query#getSingleResult() methods.

Example

Entity

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private long id;
  private String name;
  private double salary;
  private String dept;
    .............
}

Executing update queries

public class ExampleMain {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEmployees();
          findAllEmployees();
          updateITEmployeesSalaries();
          findAllEmployees();
          updateAllEmployeesSalaries();
          findAllEmployees();
      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEmployees() {
      Employee employee1 = Employee.create("Diana", 2000, "IT");
      Employee employee2 = Employee.create("Rose", 3500, "Admin");
      Employee employee3 = Employee.create("Denise", 2500, "Admin");
      Employee employee4 = Employee.create("Mike", 4000, "IT");
      Employee employee5 = Employee.create("Linda", 4500, "Sales");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(employee1);
      em.persist(employee2);
      em.persist(employee3);
      em.persist(employee4);
      em.persist(employee5);
      em.getTransaction().commit();
      em.close();
  }

  private static void findAllEmployees() {
      EntityManager em = entityManagerFactory.createEntityManager();
      System.out.println("-- All employees --");
      Query query = em.createQuery(
              "SELECT e FROM Employee e");
      List<Employee> resultList = query.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  private static void updateITEmployeesSalaries() {
      System.out.println("-- increasing IT employees salaries by 500 --");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      Query query = em.createQuery("UPDATE Employee e SET e.salary = e.salary + :increment "
              + "WHERE e.dept = :dept");
      query.setParameter("increment", 500d);
      query.setParameter("dept", "IT");
      int rowsUpdated = query.executeUpdate();
      System.out.println("entities Updated: " + rowsUpdated);
      em.getTransaction().commit();
      em.close();
  }

  private static void updateAllEmployeesSalaries() {
      System.out.println("-- increasing all employees salaries by different percentage --");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      Query query = em.createQuery("UPDATE Employee e SET e.salary = "
              + " CASE "
              + " WHEN e.dept = 'IT' THEN (e.salary * 1.20) "
              + " WHEN e.dept = 'Admin' THEN (e.salary * 1.15) "
              + " ELSE (e.salary * 1.12) "
              + " END");
      int rowsUpdated = query.executeUpdate();
      System.out.println("entities Updated: " + rowsUpdated);
      em.getTransaction().commit();
      em.close();
  }
}
-- All employees --
Employee{id=1, name='Diana', salary=2000.0, dept='IT'}
Employee{id=2, name='Rose', salary=3500.0, dept='Admin'}
Employee{id=3, name='Denise', salary=2500.0, dept='Admin'}
Employee{id=4, name='Mike', salary=4000.0, dept='IT'}
Employee{id=5, name='Linda', salary=4500.0, dept='Sales'}
-- increasing IT employees salaries by 500 --
entities Updated: 2
-- All employees --
Employee{id=1, name='Diana', salary=2500.0, dept='IT'}
Employee{id=2, name='Rose', salary=3500.0, dept='Admin'}
Employee{id=3, name='Denise', salary=2500.0, dept='Admin'}
Employee{id=4, name='Mike', salary=4500.0, dept='IT'}
Employee{id=5, name='Linda', salary=4500.0, dept='Sales'}
-- increasing all employees salaries by different percentage --
entities Updated: 5
-- All employees --
Employee{id=1, name='Diana', salary=3000.0, dept='IT'}
Employee{id=2, name='Rose', salary=4024.9999999999995, dept='Admin'}
Employee{id=3, name='Denise', salary=2875.0, dept='Admin'}
Employee{id=4, name='Mike', salary=5400.0, dept='IT'}
Employee{id=5, name='Linda', salary=5040.000000000001, dept='Sales'}

Example Project

Dependencies and Technologies Used:

  • h2 1.4.197: H2 Database Engine.
  • hibernate-core 5.3.1.Final: Hibernate's core ORM functionality.
    Implements javax.persistence:javax.persistence-api version 2.2
  • JDK 1.8
  • Maven 3.5.4

JPQL UPDATE Statements Select All Download
  • jpql-update-statement
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also