#
Execute SELECT on an Oracle table (with Hibernate)
This tutorial shows you an example of how to select rows into a Java application from 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.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("");
}
System.out.println("The Hibernate example in Java: program completed");
}
}
This code will select a row from the EMP table and will show the result to the console. Here is the result of the example:
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).