<<< Prev

Next >>>

Hibernate using annotation

We can create hibernate application by using annotation. There are many annotation that is used to create hibernate application. Like @Entity, @Id, @Table etc.

Hibernate Annotations are based on the JPA 2 specification and supports all the features.

All the JPA annotations are defined in the javax.persistence package. Hibernate EntityManager implements the interfaces and life cycle defined by the JPA specification.

Note : The advantage of this annotation is we do not need to create mapping file (hbm).In this case hibernate annotations are used to provide the meta data.

Please follow these steps to create maven projects with annotation.

1.Create the Maven Project.- File-> New -> Maven Project.

2.Add project information and configuration in pom.xml file.- Open your pom file and add dependencies. (i.e you do not need to add jars. It will download automatically from internet.)

<project xmlns="" xmlns:xsi=""







3. Create the Persistence class. – Here we will create same class as previously we did but here we will use annotation (@).

package com.qualde.Hbannotation;

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

public class Student {
private int id;
public int getId() {
	return id;
public void setId(int 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;
private String firstName,lastName;  


     4.Create the Configuration file: 


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
        <property name="">update</property>  
       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stu1</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property> 
    <mapping class="com.qualde.Hbannotation.Student"/> 

 5.Create the class that retrieves or stores the persistent object.

package com.qualde.Hbannotation;
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.boot.Metadata;  
import org.hibernate.boot.MetadataSources;  
import org.hibernate.boot.registry.StandardServiceRegistry;  
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
public class StoreData {  
    public static void main( String[] args )  
         StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
            Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
        SessionFactory factory = meta.getSessionFactoryBuilder().build();  
        Session session = factory.openSession();  
        Transaction t = session.beginTransaction();  
         Student s1=new Student();    
       System.out.println("successfully saved");    

 6.Project structure will look like this 

                                         Finally run your project.