JPA Delete Query Example

We  already discussed the fundamentals of JPA .We have discussed few examples also. In the just previous chapter we discussed  the JPA update query  with an example. In this chapter we are discussing the JPA delete Query with an example.

JPA Delete Query Example

We are using Open JPA as our JPA Implementation.The steps to create a JPA project in eclipse is explained earlier. We need to do the same steps here also, if we need  to start a new JPA project from scratch. We are reusing the same entity class( and the same persistence.xml.We discussed the database creation  and table creation also.

JPQL Delete Query can be either Selective Delete Query or Delete All Query. In this chapter we are discussing a selective query with  example. A selective query is nothing but it is acting on rows which satisfies a particular condition. A delete All query , is acting on the entire rows of the mentioned table.

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity(name = "Student")
public class Student implements Serializable {

private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String level;

public Student() {


public int getId() {
return id;

public void setId(int id) { = id;

public String getName() {
return name;

@Column(name = "name")
public void setName(String name) { = name;

public String getLevel() {
return level;

@Column(name = "level")
public void setLevel(String level) {
this.level = level;

public String toString(){
return "ID = "+getId() + " ; Name : = "+getName() +" ; Level : = "+getLevel();



org.apache.openjpa.persistence.PersistenceProviderImpl com.jpa.entity.Student

Now let us see the class with main.

import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.jpa.entity.Student;

public class DeleteSample {

public void doRead() {
EntityManagerFactory entityManagerFactory = Persistence
EntityManager entityManager = entityManagerFactory
if (null != entityManager) {
EntityTransaction readTransaction = entityManager.getTransaction();
Query query = entityManager
.createQuery("select student FROM Student student");
List list = query.getResultList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Student student = (Student);
System.out.println("Student Details : " + student);

public void doDelete() {
EntityManagerFactory entityManagerFactory = Persistence
EntityManager entityManager = entityManagerFactory
if (null != entityManager) {
EntityTransaction deleteTransaction = entityManager
Query query = entityManager
.createQuery("DELETE FROM Student student WHERE =1");
int deletionCount = query.executeUpdate();
if (deletionCount > 0) {



public static void main(String[] args) {
DeleteSample sample = new DeleteSample();
System.out.println("Before deletion..");
System.out.println("After deletion..");

The query we used is DELETE FROM Student student WHERE =1. It simply deletes a row from table with id value 1.

JPQL Delete All Query

If we simply use the query DELETE FROM Student student the entire rows of the table will be deleted.


Run the The row with id value 1 will be deleted.

See Related Discussions

JPA Overview

JPA Example – Insert/Read

The JPA find()  method

Updating record using JPA

JPA Delete Example


Update Query Example


Overview to JPA Caching

Locking in JPA

Locking in JPA

Relational Mappings in JPA

One To One Mapping in JPA

One To Many mapping in JPA

Many To Many Mapping in JPA

Many To One Mapping in JPA

Leave a Reply

Your email address will not be published. Required fields are marked *