Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Hibernate 4 Annotation Example

In this tutorial you will learn about how to make a project in Hibernate using Annotation.


How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code

Hibernate 4 Annotation Example

In this tutorial you will learn about how to make a project in Hibernate using Annotation.

To create an example of Hibernate using annotation reader should have aware with some basic terms. You can know about these terms from here. After letting know the basic terms that are useful for creating a project proceed from here. In the basic terms you will have read that the POJO class will be changed after using annotation lets see the changes occurred in POJO class.

Here I have created POJO java class named using the Annotations (may be called Entity class).


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

@Table (name= "person")
public class Person
@Column(name = "Id")
private int id;

@Column(name = "Name")
private String name;

public Person()

public Person(int id, String name) {
super(); = id; = name;
public int getId() {
return id;
public void setId(int id) { = id;
public String getName() {
return name;
public void setName(String name) { = name;

Here is the video instruction of writing and running the Hibernate Annotation example:

You can download the source code of the above video tutorial from the page Hibernate 4 Annotations Tutorial.

Next I have created class named PersonFactory that helps in to create a SessionFactory from the Hibernate configuration file.


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

public class PersonFactory {
private static ServiceRegistry serviceRegistry;

private static final SessionFactory sessionFactory;
static {
try {
Configuration configuration = new Configuration();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);

public static SessionFactory getSessionFactory() {
return sessionFactory;

Created a hibernate.cfg.xml file that the hibernate utilize to create a connection pool and the required environment setup.

<?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 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="" />

Finally develop the code by writing a java class named PersonDetail that persist the data to the database.


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class PersonDetail
	private static SessionFactory sessionFactory = null;
	public static void main(String[] args)
		Session session = null;
				PersonFactory factory = new PersonFactory();
				sessionFactory = factory.getSessionFactory();
				session = sessionFactory.openSession();
				Person person = new Person();
				System.out.println("Inserting Record");
				Transaction tx = session.beginTransaction();
			catch (Exception e)

Output :

When you will execute the PersonDetail class following output will displayed as :

1. Output on console will be as :

2. Output of data that will be saved to the database table after executing the code successfully.

Download Source Code


Share on Google+Share on Google+

Hibernate 4 Annotation Example

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



Discuss: Hibernate 4 Annotation Example  

Post your Comment

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