#
Execute INSERT on an Oracle table (with Hibernate)
This tutorial shows you an example of inserting a row 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 usage of JPA (Hibernate):
package jpa_package;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
public class MainClass {
public static void main(String[] args) {
Session session = HibernateUtility.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
String sql = "INSERT INTO EMP(ID, name, job) VALUES (:idVal, :nameVal, :jobVal)";
Query query = session.createSQLQuery(sql);
query.setParameter("idVal", "22");
query.setParameter("nameVal", "Jerome");
query.setParameter("jobVal", "DBA");
query.executeUpdate();
session.getTransaction().commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
This code will insert a row into the EMP table and will query a row from the same table.
Info
The usage of createSQLQuery is deprecated in Hibernate.
Info
When you operate with Hibernate you can use 2 methods: one closer to the Hibernate philosophy (for instance using update, delete method) and another method using the Hibernate Query Language (HQL).