Hibernate Annotations

This tutorial describes the concept of annotations.


 

Ads

Tutorials   
Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java 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 ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released 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 ORM 4.3.0.Final released 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
Ads

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.io.Serializable;
import java.util.Date;

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

public Employee() {

}

@Id
@GeneratedValue
@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) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.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.

Advertisements

Share on Google+Share on Google+

Hibernate Annotations

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

Advertisements

 

Discuss: Hibernate Annotations  

Post your Comment


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

Ads

 

Ads