First Hibernate Application Using Annotation

  • Facebook
  • Google
  • LinkedIn
  • Twitter

You will configure hibernate in eclipse and write your first hibernate program. For configuring hibernate, there are some prerequisites which you need to have on your system.

  • Download the latest hibernate framework.
  • Download any database which you need.
  • Download JDBC driver for database.
  • Install Eclipse or STS or other ide.
  • download JDK 1.5 or above.

Here are the files which required to hibernate program

  • Student.java
  • hibernate.cfg.xml
  • HibernateMain.java

Student.java

package entity;

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

@Entity
@Table(name = "STUDENT")
public class Student {
      @Id
      @Column(name = "ID")
      private int id;
        
      @Column(name = "NAME")
      private String name;
        
      @Column(name = "DEPARTMENT")
      private String department;
     
	 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 String getDepartment() {  
  		return department;  
 	}  
	 public void setDepartment(String department) {  
  		this. department = department;  
 	}

}

Create a new XML file and give this new configuration file the default name hibernate.cfg.xml and place it src directory of your project.

Hibernate Configuration file hibernate.cfg.xml

 

<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd
 /hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
 
 <property name="hibernate.connection.driver_class">
	oracle.jdbc.driver.OracleDriver
 </property>
 <property name="hibernate.connection.username">
 	admin
 </property>
 <property name="hibernate.connection.password">
 	admin
 </property>
 <property name="hibernate.connection.url">
    jdbc:oracle:thin:@xxx.x.x.x:1521:xx
 </property>

 <!-- SQL dialect -->
 <property name="hibernate.dialect">
 	org.hibernate.dialect.Oracle10gDialect
 </property>

 <!-- Echo all executed SQL to sysout -->
 <property name="show_sql">true</property>

 <!-- Drop and re-create the database schema on startup -->
 <property name="hibernate.hbm2ddl.auto">
 	update
 </property>
 <!-- Map Entity Class -->
 <mapping class="entity.Student"></mapping>

</session-factory>
</hibernate-configuration>

Create the Main class to run the example.

HibernateMain.java

 
package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.
StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import entity.Student;

public class HibernateMain {

  public static void main(String[] args) {

  Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

  StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();
  
  srb.applySettings(cfg.getProperties());
  
  ServiceRegistry sr = srb.build();
  
  SessionFactory sf = cf.buildSessionFactory(sr);

  Session session = sf.openSession();
    
  StudentEntity std = new StudentEntity();
  
  std.setId(2);
  std.setName("Ram");
  std.setDepartment("Computer");
  
  Transaction tx = session.beginTransaction();
  session.save(std);
  tx.commit();
  
  System.out.println("Object saved successfully.");
  
  session.close();
  sf.close();
  }
}

Output:

 
Object saved successfully.

2	Ram	Computer
      Advertisements