Share on Google+Share on Google+

Hibernate Annotations

This tutorial describes the concept of annotations.

Hibernate Annotations

This tutorial describes the concept of annotations.

Concept of Annotation :

Annotation is the way of using data of program that is not part of your current program. It does not directly affect the code but provide the feature of annotated class.

Some of annotation uses mentioned here-

  • Compiler can use annotation for detecting errors or suppress warnings.
  • Annotation can be processed by software tools to generate code,  xml file etc.
  • Some annotations are examined at runtime.

Hibernate Annotations:

Before using hibernate annotation make sure that you have JDK5 or higher and hibernate 3.2 or higher. User must add Hibernate-Annotations jar file.

 Hibernate needs a metadata to govern the transformation of data from POJO to database tables and vice versa. There is two way to handle this problem.

1. Hibernate hbm.xml

2. Hibernate annotation

 Hibernate provides specific annotations that match hibernate features. The org.hibernate.annotations package contains all these annotations extensions.

Example: This example show how to use annotation in your persistence class.

package net.roseindia.table;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Table(name = "employee")
public class Employee implements Serializable {

public Employee() {


@Column(name = "emp_id")
private int id;

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

@Column(name = "salary")
private int salary;

@Column(name = "date_of_join")
private Date dateOfJoin;

public int getId() {
return id;

public void setId(int id) { = id;

public String getName() {
return name;

public void setName(String name) { = name;

public int getSalary() {
return salary;

public void setSalary(int salary) {
this.salary = salary;

public Date getDateOfJoin() {
return dateOfJoin;

public void setDateOfJoin(Date dateOfJoin) {
this.dateOfJoin = dateOfJoin;


Hibernate annotation is used for object relational mappping.Hibernate supports EJB 3 persistaence annotations,
included in javax.persistence package.

 @Entity annotation- makes your POJO persistence entity bean.

 @Table annotation- specify the table to persist the data.It contain name attribute which refer the table name in database.
as in our example @Table(name="employee") annotation tells the entity is mapped with the table employee.

 @Id annotation- shows the idenifier property of the entity bean.
It tells that the mapped property refer to the primary key of table column in database.

@GeneratedValue annotation-used to show that the primary key generation strategy to use.

@Column annotation- It maps table column to coresponding field or properties.



Posted on: June 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 Annotations  

Post your Comment

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