Hibernate Basics



  • It is a framework developed by Red Hat.

  • Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.

  • An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.

  • The ORM tool internally uses the JDBC API to interact with the database.

Java By Kiran


  • Hibernate is slower than JDBC but development is faster in hibernate

  • Hibernate is suitable for database migration e.g. from oracle to mysql.

  • For database migration it uses Dialect.

Let’s create simple client.java in which we will access table from mysql using hibernate.

Required things-

  • Jars – hibernate jars
  • Files –
      1. Student.java (pojo/Form bean)
      2. Client.java
      3. student.hbm.xml(mapping file for student)
      4. hibernate.cfg.xml
  • ‘Student’ table in database

Student.java –
  • It is POJO/form bean class.
  • It contains all the column names of table Student and its getters and setters.
  • It is not compulsory to have same names as column names because we have mapping in hbm file (i.e.Student.hbm.xml)


student.hbm.xml –
These are the hbm files for each table name.
  • E.g. Student <---> student.hbm.xml
  • Employee <---> employee.hbm.xml

hibernate.cfg.xml –
  • It is one file for single project
  • It contain all hbm paths
  • The configuration file contains all the informations for the database such as connection_url, driver_class, username, password etc.

Student.java –


package com.arjun;

public class Student {

	int id;
	String fname,lname;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFname() {
		return fname;
	}
	public void setFname(String fname) {
		this.fname = fname;
	}
	public String getLname() {
		return lname;
	}
	public void setLname(String lname) {
		this.lname = lname;
	}
}



Student.hbb.xml –



< ? xml version="1.0" encoding="UTF-8"? >

< ! DOCTYPE hibernate-mapping PUBLIC   
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  

< hibernate-mapping >
	
        < id name ="id" / >
        < property name="fname" > < / property >   
        < property name="lname" > < / property >   
        
        
	

< / hibernate-mapping >


Hibernate.cfg.xml –


< ? 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" >   

< hibernate-configuration >

	< session-factory >
	
	 < 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 >  
	
	< / session-factory>

< / hibernate-configuration >



Client.java –

package com.arjun;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class Client {

	public static void main(String[] args) {
		
		 //creating configuration object   
		    Configuration cfg=new Configuration();   
		    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file   
       System.out.println("after config...");
		
       //creating seession factory object   
		    SessionFactory factory=cfg.buildSessionFactory();   
	    System.out.println("after sessfact...");
		
	    
	    //creating session object   
		   Session session=factory.openSession();   
	    
		   //featching data from table
		
		   Student s =(Student)session.load(Student.class, 1);
		    System.out.println(s.getId()+s.getFname());   
		    
		    //insert data into db
		    
		    Transaction t = session.beginTransaction();
		    
		    Student ss = new Student();
		    ss.setId(6);
		    ss.setFname("arj");
		    ss.setLname("bh");
		    		
		    session.save(ss);
		    
		    t.commit();
		    System.out.println("after inserting..");
		}
	
}


Crete student table