Close

JPQL DELETE Statements

[Updated: Jul 3, 2018, Created: Jul 3, 2018]

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

DELETE FROM entity_name [[AS] identification_variable] [where_clause] 

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

Example

Entity

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

Executing DELETE query

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

  public static void main(String[] args) {
      try {
          persistEmployees();
          findAllEmployees();
          deleteEmployeeByName();
          findAllEmployees();
          deleteAllEmployees();
          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();
      System.out.println("Employees count: "+resultList.size());
      resultList.forEach(System.out::println);
      em.close();
  }

  private static void deleteEmployeeByName() {
      System.out.println("-- delete employee by name 'Mike' --");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      Query query = em.createQuery("DELETE FROM Employee e WHERE e.name = :employeeName ");
      query.setParameter("employeeName", "Mike");
      int rowsDeleted = query.executeUpdate();
      System.out.println("entities deleted: " + rowsDeleted);
      em.getTransaction().commit();
      em.close();
  }

  private static void deleteAllEmployees() {
      System.out.println("-- delete all employees --");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      Query query = em.createQuery("DELETE FROM Employee e ");
      int rowsDeleted = query.executeUpdate();
      System.out.println("entities deleted: " + rowsDeleted);
      em.getTransaction().commit();
      em.close();
  }
}
-- All employees --
Employees count: 5
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'}
-- delete employee by name 'Mike' --
entities deleted: 1
-- All employees --
Employees count: 4
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=5, name='Linda', salary=4500.0, dept='Sales'}
-- delete all employees --
entities deleted: 4
-- All employees --
Employees count: 0

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 DELETE Statements Select All Download
  • jpql-delete-statement
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF

See Also