# Execute DELETE on an Oracle table (with Hibernate)

In 
JPA
Published 2022-12-03

This tutorial shows you an example of how to delete rows from a Java application into an Oracle database using Hibernate as JPA implementation.

When you want to use Hibernate as JPA, you have to configure Hibernate into your Java application.

Even if the HibernateUtility class is not a configuration of Hibernate, such a class in useful in your Java code when using Hibernate. It helps you to create the SessionFactory object, to log the exceptions and to close caches and connection pools:

package jpa_package;
 
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class HibernateUtility {
 
    private static final SessionFactory sessionFactory = buildSessionFactory();
 
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory (from hibernate.cfg.xml)
            return new Configuration().configure().buildSessionFactory();
             
        } catch (Throwable ex) {
            //Log the exception
            System.err.println("Initial SessionFactory creation failed with the following error: " + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
 
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
 
    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }
}

You need also to create a table in the database. In my example I use an Oracle database. See the code below.

CREATE TABLE scott.EMP(
  ID     NUMBER(10)   primary key,
  NAME   VARCHAR2 (20)  NOT NULL,
  JOB    VARCHAR2 (20)  NOT NULL
);

We need to create a class in order to keep the column values related to a table row:

package jpa_package;
 
public class Employee {
 
    private int id;
    private String name;
    private String job;
     
    public Employee() { }
     
    public Employee(int id, String name, String job) {
        super();
        this.id = id;
        this.name = name;
        this.job = job;
    }
     
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
}

I will create a main class for showing the how to delete a row using JPA (Hibernate):

package jpa_package;
  
import org.hibernate.Session;
import org.hibernate.query.Query;
  
import java.util.Iterator;
import java.util.List; 
  
public class MainClass {
    public static void main(String[] args) {
          
        System.out.println("The Hibernate example in Java: start");
        Session session = HibernateUtility.getSessionFactory().openSession();
          
        session.beginTransaction();
         
        Employee emp = new Employee();
         
        // Get the Employee with the ID = 22
        emp = session.get(Employee.class, 22); 
        session.delete(emp);
         
        session.getTransaction().commit();
         
        System.out.println("The Hibernate example in Java: program completed");
    }
}

This code will DELETE a row from the EMP table.