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(Student.java) 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.

Student.java

import java.io.Serializable;
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")
@Table(name="student")
public class Student implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@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) {
this.id = id;
}

public String getName() {
return name;
}

@Column(name = "name")
public void setName(String name) {
this.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();
}

}

persistence.xml

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

Now let us see the class with main.

DeleteSample.java

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
.createEntityManagerFactory("OpenJPASample");
EntityManager entityManager = entityManagerFactory
.createEntityManager();
if (null != entityManager) {
EntityTransaction readTransaction = entityManager.getTransaction();
readTransaction.begin();
Query query = entityManager
.createQuery("select student FROM Student student");
List list = query.getResultList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Student student = (Student) iterator.next();
System.out.println("Student Details : " + student);
}
readTransaction.commit();
}
}

public void doDelete() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("OpenJPASample");
EntityManager entityManager = entityManagerFactory
.createEntityManager();
if (null != entityManager) {
EntityTransaction deleteTransaction = entityManager
.getTransaction();
deleteTransaction.begin();
Query query = entityManager
.createQuery("DELETE FROM Student student WHERE student.id =1");
int deletionCount = query.executeUpdate();
if (deletionCount > 0) {
System.out.println("Done..");
}
deleteTransaction.commit();

}

}

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

The query we used is DELETE FROM Student student WHERE student.id =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.

Output

Run the DeleteSample.java. 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

JPQL

Update Query Example

Caching

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