# Execute UPDATE on an Oracle table (with Hibernate)

In 
JPA
Published 2022-12-03

This tutorial shows you an example of how to update 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:

HibernateUtility.java
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:

Employee.java
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;
    }
}

This code will update a row into the EMP table and will query the row again in order to see the difference.

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();

    Query query = session.createQuery("FROM Employee where id=:qid");
    query.setParameter("qid", 20);

    List list = query.list();

    for (Iterator iterator = list.iterator(); iterator.hasNext();){
      Employee employee = (Employee) iterator.next();

      System.out.println("");
      System.out.println("ID: " + employee.getId());
      System.out.println("Name: " + employee.getName());
      System.out.println("Job: " + employee.getJob());
      System.out.println("");
    }

    session.beginTransaction();

    Employee emp = new Employee();

    // Get the Employee with the ID = 20
    emp = session.get(Employee.class, 20);
    emp.setJob("Oracle Architect");
    session.update(emp);

    session.getTransaction().commit();

    for (Iterator iterator = list.iterator(); iterator.hasNext();){
      Employee employee = (Employee) iterator.next();

      System.out.println("");
      System.out.println("ID: " + employee.getId());
      System.out.println("Name: " + employee.getName());
      System.out.println("Job: " + employee.getJob());
      System.out.println("");
    }

    System.out.println("The Hibernate example in Java: program completed");
  }
}

Here is the result of the example: