Hibernate - SPLessons

Chapter 9

Hibernate Annotation

Hibernate Annotation

Hibernate Annotations are used to reduce the code of an application and the main advantage of an annotation is that no need to write mapping file, Annotation tags will be represented with “@”, The following are the tags which will be used in the code. Hibernate Annotation will carry meta data that will also be used in the code.


It means the Persistence class is an entity. This Hibernate Annotation is represented in class-level.


@Table it is also class level hibernate annotation, Here if the developer did not mention any table name, then by default it will be taken to the table name.


@Id is represented on Property level or getter method level annotation. It is used for mapping a POJO class variable and Primary Key Column of the DataBase table.


@Column is to map with a property column of DataBase table. If property name and column name same then @Column is optional. If the developer did not mention any column, then by default it will be taken.

Following are the required files to execute the hibernate application, Here hibernate mapping file is not needed.


package com.abc;
import javax.persistence.Entity;  
import javax.persistence.Id;  
import javax.persistence.Table;  
@Table(name= "emp500")  
public class Employeeannotation {  
private int id;  
private String firstName,lastName;  
public int getId() {  
    return id;  
public void setId(int id) {  
    this.id = id;  
public String getFirstName() {  
    return firstName;  
public void setFirstName(String firstName) {  
    this.firstName = firstName;  
public String getLastName() {  
    return lastName;  
public void setLastName(String lastName) {  
    this.lastName = lastName;  

The @Table annotation permits the programmer to determine the points of interest of the table that will be utilized to continue the entity in the database.The @Table comment gives four characteristics, permitting the programmer to supersede the name of the table, its index, and its mapping, and uphold one of a kind limitations on segments in the table.
Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them. By encapsulating them in this manner, one can have control over the public interface.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->

        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <mapping class="com.abc.Employeeannotation"/>  

Properties Description
hibernate.connection.driver_class The JDBC driver class.
hibernate.dialect This property makes Hibernate generate the suitable SQL for the picked database.
hibernate.connection.url The JDBC URL to the database instance.
hibernate.connection.username The database username.
hibernate.connection.password The database password.
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.

Here Write <mapping class=”com.abc.Employeeannotation”/> to identify the file.

package com.abc;

import org.hibernate.*;  
import org.hibernate.cfg.*;  
public class persistencestored {  
public static void main(String[] args) {  
    Session session=new AnnotationConfiguration()  
    Transaction t=session.beginTransaction();  
    Employeeannotation e1=new Employeeannotation();  
    Employeeannotation e2=new Employeeannotation();  
    System.out.println("successfully saved");  

The commit() treats a solitary SQL statement or a gathering of SQL explanations as one logical unit, and if the statement fails, the entire transaction comes up short. To empower manual-transaction support rather than the auto-commit mode that the JDBC driver utilizes uses by default.
Output will be as follows, The following message will be displayed and table will be created automatically in MySQL.

As inserted records in the coding output will be as follows.


  • If the developer use hibernate annotation, Then no need to use mapping file.
  • Class name should represent in configuration file, Then it can identify easily.
  • If not use @table tag, Table name will be created by default.