Using the Delete query

Deleting records from a database is a common activity. We can use JPQL to delete individual records or multiple records. JPQL query deals with entities as opposed to database records. In this recipe we will explore the process of deleting entities.

Getting ready

The basic form of the Delete query consists of:

DELETE FROM entity entityIdentificationVariable WHERE condition

For example, to delete the entity whose name is "Donald Baker" we would use the query.

DELETE FROM Patient p WHERE p.firstName = 'Donald' AND p.lastName = 'Baker'

The steps used to create and use a JPQL Delete query include:

  1. Obtaining an instance of an EntityManager
  2. Using the createQuery method to create an instance of a Query based on a Delete JPQL string ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.