Hibernate Select Query

Hibernate Select Query
In previous article we learnt to implement Hibernate 4 Insert Query example in Eclipse, In this article I will implement an example on loading the object from the database(Select Query) using Hibernate.

Project Structure


Entity class


package entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "STUDENT")
public class StudentEntity {

	@Column(name = "ID")
	private int id;
	@Column(name = "NAME")
	private String name;
	@Column(name = "DEPARTMENT")
	private String department;
	@Column(name = "COLLEGE")
	private String college;

// Create Getters and Setters

Note: I have explained about every annotations used in the above file in the article Generic Hibernate Application Requirements

Hibernate Configuration file

Create a new XML file and give this new configuration file the default name hibernate.cfg.xml and place it src directory of your project.
File: hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"


	<!-- Database connection settings -->
	<property name="hibernate.connection.driver_class">
	<property name="hibernate.connection.username">system</property>
	<property name="hibernate.connection.password">admin</property>
	<property name="hibernate.connection.url">

	<!-- SQL dialect -->
	<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

	<!-- Echo all executed SQL to sysout -->
	<property name="show_sql">true</property>

	<!-- Drop and re-create the database schema on startup -->
	<property name="hibernate.hbm2ddl.auto">update</property>
	<!-- Map Entity Class -->
    <mapping class="entity.StudentEntity"></mapping>



Hibernate Utility


package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import entity.StudentEntity;

public class HibernateUtil {

   public static void main(String[] args) {

     Configuration cf = new Configuration().configure("hibernate.cfg.xml");
     StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();
     ServiceRegistry sr = srb.build();
     SessionFactory sf = cf.buildSessionFactory(sr);

     Session session = sf.openSession();
     StudentEntity std = (StudentEntity) session.load(StudentEntity.class, new Integer(1));

     // For loading Transaction object is not necessary
     System.out.println("Loaded object Student name is: " + std.getName());

     System.out.println("Object Loaded successfully.....!!");

In the article Steps to be followed to use Hibernate in Java, I have explained the configuration/code used in above program in detail.

Data in Database

Hibernate DB

Run it – Eclipse Console

Hibernate Select Query


In hibernate we have 2 methods to load the object from the database, they are get and load. To know the difference between get and load method read the article here.

About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.