JPA Delete example

We already discussed the fundamentals of Java persistence API.In this chapter we are discussing how an entry can be deleted from table using JPA. Alternately We can delete an entry using the delete query also.

JPA Delete example

We are using OpenJPA as implementation.We already  discussed the  steps in configuring OpenJPA with eclipse and MySQL in an earlier chapter.We are using the same set up here also.The same  entity class and persistence.xml are using here also.

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 main class . It simply deletes an existing record.We are trying to delete a Student object from the database table with id value 301 .

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 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();
Student student = entityManager.find(Student.class, 301);
if (null != student) {
entityManager.remove(student);
System.out.println("Deleted..");
}
deleteTransaction.commit();
}
}

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

}

Lets see the output.

Output

Run the  DeleteSample.java.

Before Delete..Student Details : ID = 301 ; Name : = Noby ; Level : = H

Student Details : ID = 302 ; Name : = Karthik ; Level : = U

Student Details : ID = 303 ; Name : = Bijoy ; Level : = L

Deleted..

After deletion..

Student Details : ID = 302 ; Name : = Karthik ; Level : = U

Student Details : ID = 303 ; Name : = Bijoy ; Level : = L

See Related Discussions

JPA Overview

JPA Example – Insert/Read

find method in JPA example

JPA Update Example

JPQL

JPQL Update Query Example

JPQL Delete Query

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

Leave a Reply

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