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.

ORM Hibernate Diagram

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. 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 mapped with the help of annotation @Column.

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.

Jbkstudent table -

CREATE TABLE `jbkstudent` (
  `sid` int NOT NULL,
  `firstname` varchar(45) DEFAULT NULL,
  `lastname` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`sid`)
);

Jbkstudent.java –

package com.javabykiran.hibernate;
// Generated Feb 24, 2020 6:23:34 PM by Hibernate Tools 5.2.12.Final

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

/**
 * Jbkstudent generated by hbm2java
 */
@Entity
@Table(name = "jbkstudent")
public class Jbkstudent implements java.io.Serializable {

    private int sid;
    private String firstname;
    private String lastname;

    public Jbkstudent() {
    }

    public Jbkstudent(int sid) {
	this.sid = sid;
    }

    public Jbkstudent(int sid, String firstname, String lastname) {
	this.sid = sid;
	this.firstname = firstname;
	this.lastname = lastname;
    }

    @Id
    @Column(name = "sid", unique = true, nullable = false)
    public int getSid() {
	return this.sid;
    }

    public void setSid(int sid) {
	this.sid = sid;
    }

    @Column(name = "firstname", length = 45)
    public String getFirstname() {
	return this.firstname;
    }

    public void setFirstname(String firstname) {
	this.firstname = firstname;
    }

    @Column(name = "lastname", length = 45)
    public String getLastname() {
	return this.lastname;
    }

    public void setLastname(String lastname) {
	this.lastname = lastname;
    }
}

Hibernate.cfg.xml –

<?xml version="1.0" encoding="UTF-8"?>
<!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">com.mysql.jdbc.Driver</property>
	<property name="hibernate.connection.password">root</property>
	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jbkhibernate</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

JbkClient.java

package com.javabykiran.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class JbkClient {
    public static void main(String[] args) {

	Configuration cfg = new Configuration();
	cfg.configure().addAnnotatedClass(Jbkstudent.class);

	// creating SessionFactory Object
	SessionFactory sf = cfg.buildSessionFactory();

	// creating Session Object
	Session session = sf.openSession();

	// Fetching data from table
	Jbkstudent stu = session.load(Jbkstudent.class, 1);
	System.out.println(stu.getSid() + " " + stu.getFirstname() + " " + stu.getLastname());

	// Insert data into table
	session.beginTransaction();
	Jbkstudent jbk = new Jbkstudent(2, "JavaByKiran", "JavaByKiran");
	session.save(jbk);
        session.getTransaction().commit();
    }
}