Hibernate Hello World example using Annotation

In this tutorial we will write a CRUD application in Hibernate using Java 5 Annotation. For this we will use our previous tutorial Hibernate Maven MySQL hello world example (XML Mapping) as base and convert it from XML Mapping to Annotation.

Tools and Technologies used:

  1. Java JDK 5 or above
  2. Eclipse IDE 3.2 or above
  3. Maven 3.0 or above
  4. Hibernate 3.0 or above
  5. MySQL 5.0 or above

1. Database Creation

For this tutorial, we will create a simple table “employee” in MySQL. Use following script to create the table.

CREATE TABLE `employee` (
    `firstname` VARCHAR(50) NULL DEFAULT NULL,
    `lastname` VARCHAR(50) NULL DEFAULT NULL,
    `birth_date` DATE NOT NULL,
    `cell_phone` VARCHAR(15) NOT NULL,
    PRIMARY KEY (`id`)

2. Create Project Structure

Follow the steps in Hibernate Maven MySQL hello world example (XML Mapping) to create project structure.

Or you can directly download the source code from above tutorial in Eclipse.

Download: HibernateHelloWorldXML.zip (8 kb)

3. Update Hibernate dependency in Maven pom.xml

We are going to use Annotation mapping in Hibernate. Update the maven’s pom.xml file and add following dependencies.


Once we update the Hibernate dependency, run following command in your project folder to update classpath in eclipse to that errors are removed.

mvn eclipse:eclipse

4. Delete unused Employee.hbm.xml

The Employee.hbm.xml file under /src/main/java/net/viralpatel/hibernate is no more required. Delete this file from project.

5. Update Employee entity

The Employee class is simple POJO. We will add Annotations in this class. Update it as follows:

package net.viralpatel.hibernate;

import java.sql.Date;

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

public class Employee {

	private Long id;
	private String firstname;
	private String lastname;
	private Date birthDate;
	private String cellphone;

	public Employee() {
	public Employee(String firstname, String lastname, Date birthdate, String phone) {
		this.firstname = firstname;
		this.lastname = lastname;
		this.birthDate = birthdate;
		this.cellphone = phone;
	// Getter and Setter methods

6. Update Hibernate Utility

In our previous example, we were using Configuration class to generate SessionFactory object. In HibernateUtil we had following line of code:

return new Configuration()

Change this line and use AnnotationConfiguration instead of Configuration(). Replace this line with below code.

// import org.hibernate.cfg.AnnotationConfiguration;

return new AnnotationConfiguration()

7. Update Hibernate Configuration file

The hibernate.cfg.xml configuration file needs to be changed to add the new Annotation based entity class Employee.java instead of old XML Mapping Employee.hbm.xml.

Open /src/main/resources/hibernate.cfg.xml and replace following line:

Replace above line with following:

8. Review Final Project Structure

Review your project structure. It should be like:

9. Execute project

Execute the Main.java class and see output.

******* READ *******
Total Employees: 200
******* WRITE *******
201 Jack Bauer
******* UPDATE *******
Name Before Update:Paula
Name Aftere Update:James
******* DELETE *******

That’s All Folks

Today we saw how to write our first Hibernate hello world example using Annotations. We used Maven to generate Java project and added Hibernate dependencies into it. Also we saw how to do different CRUD operations in Hibernate.

Download Source

HibernateHelloWorldAnnotation.zip (15 kb)


  • Karthik 16 December, 2011, 17:56

    Awesome Tutorial.
    In pom.xml, currently, the annotations, JPA etc.. are part of hibernate-core dependency.
    Please do update Dependencies in pom.xml to:
    (Remove extra “”). Some issues with posting comment here




  • jAKEER 5 July, 2012, 18:04

    thanks VP…thanks for a wonderful food .


  • kumar 27 November, 2012, 19:07

    How to connect with oracle………

  • sam 27 November, 2012, 19:09

    how to connect with oracle 10g

    • Viral Patel 27 November, 2012, 20:06

      Hi Sam, To connect with Oracle database use following settings in hibernate configuration file.

          <property name="hibernate.connection.driver_class">
          <property name="hibernate.connection.url">
          <property name="hibernate.connection.username">
          <property name="hibernate.connection.password">
          <property name="dialect">
  • ramya 18 December, 2012, 15:49

    Im not able to work with Hibernate annotations when i tried to run the project im getting Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
    Exception in thread “main” java.lang.ExceptionInInitializerError error

  • ramya 18 December, 2012, 15:50

    Can anyone share the JARS for Hibernates along with annotations hibernate official site is blocked for us in office

  • Swen Johnson 5 January, 2013, 8:00

    Source download link doesn’t work.

    • Viral Patel 5 January, 2013, 18:25

      Hi Swen, Download link is working. Please check if download is not blocked in your browser.

  • Sachin Patil 6 February, 2013, 16:05

    This is very good tutorial for beginner but only problem at adding supported jar file. otherwise will run this example by a new comer. Thank u bhaava…..

  • Vijendra Kumawat 28 February, 2013, 18:20

    Hi viral…
    Thanks for your wonderful blog.
    I am trying to integrate hibernate with JSF. Do you any example for this. I tried to google for this but did not find any useful example.

  • Felipe 20 April, 2013, 19:05

    Awesome tutorial, I looked for Hibernate project with maven on Youtube, google and finally got your blog.
    I worked perfectly.
    Thank you so much!!!!!!

  • Anup 30 July, 2013, 18:38

    I am using annotations instead of hdm files.
    Database is created properly,but whenever i hit to db server for fetching data sometime data is not getting fetched.
    Can you suggest any solutions ?
    I have also removed fetchType eager for Onetomany mappings.

  • Sefer 3 December, 2014, 6:49

    You must add core hibernate on pom.xml file.


    and you will check your dependency versions. don’t skip that ;)


  • praneeth 16 January, 2015, 14:13

    Is their a way so that i can add @Table annotation dynamically at run time.
    Value of Name attribute in Table annotation must be dynamic.
    I tried Javassist but it didn’t satisfied me. I’m losing fuctionality of hibernate in auto genertion of table if table doesn;t exist in DB.
    Pls help me.
    Thank you.

  • kapil 20 March, 2015, 12:57

    How to run that plz tell me once.

  • Hnin Yee Maung Maung 19 April, 2015, 8:56

    Thank you so much for your clear explanation. I have got so many knowledge from your website.

Leave a Reply

Your email address will not be published. Required fields are marked *


To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]