Annotation Example in Hibernate

Hibernate Annotation Example

Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses the specifications of the Java Persistence API (JPA).

Hibernate provides annotations for the ease of web development. Without using hibernate-mapping file, we can map a java class to the corresponding table with the help of JPA Annotations. Some of the JPA Annotations are listed below:

  • @Entity- It is used for making a bean class as an entity bean, so it must contain a no-arg constructor.
  • @Table- It is used to create a table for the annotated entity.
  • @Id- The primary key in the table is denoted as @Id.
  • @Generatedvalue- It is used to auto-generate the value of the @Id field.
  • @Column- It is used to define the details of the column to which an attribute is mapped.
  1. Add Dependencies

When using hibernate annotations, you need to add dependencies in pom.xml. Add dependencies between <dependencies>...</dependecies> tag. Some dependencies are listed below:


You need to add Oracle dependency manually. If you are using Mysql, then you need to add MySQL jar or dependency.

2. Create a POJO/ Bean/ Persistence class

The encapsulation of many objects into a single object is done by POJO class or Bean class.

All the variables of bean class are set as private with public setter and getter methods.

Here we use Annotations in the bean class.


import javax.persistence.Column;
 import javax.persistence.Entity; 
 import javax.persistence.GeneratedValue; 
 import javax.persistence.GenerationType; 
 import javax.persistence.Id; 
 import javax.persistence.Table; 
 public class Book {
             private int bno;
             private String bname; 
             private int price;
             public int getBno() {
                         return bno; 
             public void setBno(int bno) {
                         this.bno = bno;
             public String getBname() {
                         return bname;
             public void setBname(String bname) { 
                         this.bname = bname;
             public int getPrice() {
                         return price; 
    public void setPrice(int price) {
                         this.price = price;

3. Create a Configuration File

Let’s create a configuration file that contains information about the mapping file and a relational database.


<?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-configuration PUBLIC  
         "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
     <property name="">update</property>    
         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>    
         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> 
         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>   
         <property name="connection.username">root</property>                        //write your username
         <property name="connection.password">root</property>                       //write your password
         <mapping class="com.hibernate.pack.Book"/>  

4. Create the Application Class

    It is a class which contains the main() methodused to run the application.

    Let’s create

import org.hibernate.Session; 
import org.hibernate.SessionFactory;                                                                               
import org.hibernate.Transaction;  
public class AppTest{ 
public static void main(String[] args) {    
Configuration cfg= new Configuration();
SessionFactory factory= cfg.buildSessionFactory();
Session session = factory.openSession();  
Transaction t = session.beginTransaction();   
 Book book=new Book(); 
 book.setBname("Java Complete Reference");
  System.out.println("saved successfully ");    


Annotation Example in Hibernate

Table- book01

Hibernate annonation