Share on Google+Share on Google+

Hibernate hello world

In this section, you will learn about basic tutorial of inserting value in the database table and printing "hello world" on the console after successful insertion.

Hibernate hello world

In this section, you will learn about basic tutorial of inserting value in the database table and printing "hello world" on the  console after successful insertion.


In the below example, we are inserting a record and after successful insertion a Hibernate Hello World Example message will be displayed.

The project structure and jar file used is given below :

The query used to create employee table is given below :

CREATE TABLE `employee` ( 
`Emp_code` int(11) NOT NULL auto_increment, 
`Emp_name` varchar(30) default NULL, 
PRIMARY KEY (`Emp_code`) 


The annotated Employee entity class is given below :

package net.roseindia;

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

@Table(name = "employee")
public class Employee {
@Column(name = "Emp_code")
private int EmpCode;

@Column(name = "Emp_name")
private String EmpName;

public int getEmpCode() {
return EmpCode;

public void setEmpCode(int empCode) {
EmpCode = empCode;

public String getEmpName() {
return EmpName;

public void setEmpName(String empName) {
EmpName = empName;


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>

<mapping class="net.roseindia.Employee" />


Using this class, we have created SessionFactory  instance :

package net.roseindia;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 
return configuration.buildSessionFactory(serviceRegistry);

catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);

public static SessionFactory getSessionFactory() {
return sessionFactory;

public static void shutdown() {
// Close caches and connection pools

package net.roseindia;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class ManageEmployee {
public static void main(String args[]){
Session session=HibernateUtil.getSessionFactory().openSession();
Employee emp=new Employee();
emp.setEmpName("Rajesh Koothrapalli");;

//Displaying inserted value
Query elist = session.createQuery("from Employee as emp where emp.EmpName = 'Rajesh Koothrapalli'");
List<Employee> empList = elist.list();
for (Employee e : empList) {
System.out.println("Employee ID : " + e.getEmpCode());
System.out.println("Employee Name : " + e.getEmpName());
}catch (Exception e) {
System.out.println("Hibernate Hello World Example");


After executing code, you will get the following output in console :

Hibernate: insert into employee (Emp_name) values (?)
Hibernate: select employee0_.Emp_code as Emp1_0_, employee0_.Emp_name as Emp2_0_ from employee employee0_ where employee0_.Emp_name='Rajesh Koothrapalli'
Employee ID : 1
Employee Name : Rajesh Koothrapalli
Hibernate Hello World Example

In database table Employee , you will find the inserted record :

Download Source Code


Posted on: April 27, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate hello world  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image