0% found this document useful (0 votes)
18 views16 pages

Hibernate

The document provides an overview of Hibernate, an Object Relational Mapping (ORM) tool that maps Java objects to relational database tables and facilitates CRUD operations. It discusses the limitations of JDBC, introduces the Java Persistence API (JPA) as a specification for data management, and highlights Hibernate's features such as transparent persistence, database independence, and performance optimization. Additionally, it includes examples of Hibernate configuration and usage with a 'Student' class to demonstrate its application in Java programming.

Uploaded by

nimarjotk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views16 pages

Hibernate

The document provides an overview of Hibernate, an Object Relational Mapping (ORM) tool that maps Java objects to relational database tables and facilitates CRUD operations. It discusses the limitations of JDBC, introduces the Java Persistence API (JPA) as a specification for data management, and highlights Hibernate's features such as transparent persistence, database independence, and performance optimization. Additionally, it includes examples of Hibernate configuration and usage with a 'Student' class to demonstrate its application in Java programming.

Uploaded by

nimarjotk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Hibernate

By – Rakesh Kumar
Object Relational Mapping (ORM)
• ORM is a programming technique which is used to convert object into a
relational database.

By – Rakesh Kumar
Object Relational Mapping (ORM)
• ORM framework is Hibernate.
• Hibernate is an ORM tool which means it is used to map plain java objects to
tables of a relational database and vice versa.

By – Rakesh Kumar
Object Relational Mapping (ORM)
• CRUD operations handover the object to Hibernate.
• Hibernate uses JDBC internally to talk to the relational database for
persisting.

By – Rakesh Kumar
Limitations of JDBC
• In JDBC, if we open a database connection, we need to write in try block, and
if any exceptions occurred catch block will take care of it and finally block is
used to close the connections.
• In JDBC, programmer needs to write SQL commands at various places. If the
table structure is modified, then the JDBC program will not work.
• Programmer has to create a table explicitly.
• If a connection is not closed by the developer, then JDBC is not responsible
to close the connection.

By – Rakesh Kumar
Introduction to Java Persistence API
• Java Persistence API (JPA) is a Java specification for accessing, persisting and managing
data between Java objects/classes and a relational database. JPA acts as a bridge between
object-oriented domain models and relational database systems, making it easier for
developers to work with data in their applications.
• JPA allows developers to map Java objects to database tables and vice versa using
annotations or XML configuration files. This abstracts the complexities in converting data
between its object-oriented form in the application and its relational form in the database.
• JPA is not an implementation but a specification. Various ORM tools, such as Hibernate,
EclipseLink and Apache Open JPA, provide implementations of the JPA specification. This
allows developers to switch between these implementations if needed without changing the
application code that uses JPA.

By – Rakesh Kumar
Introduction to Hibernate
• Hibernate is a Java-based ORM tool that provides a framework for mapping
application domain objects to relational database tables and vice versa.
• It is the most popular JPA implementation and one of the most popular Java
ORM frameworks. Hibernate is an additional layer on top of JDBC and
enables the developer to implement a database independent persistence
layer.
• It provides an object relational mapping implementation that maps the
database records to Java objects and generates the required SQL statements
to replicate all operations to the database.

By – Rakesh Kumar
Object Relational Mapping

By – Rakesh Kumar
Hibernate Features
• Transparent Persistence – Hibernate manages the persistence of objects
without requiring significant changes to how those objects are designed.
• Database Independence – Applications built with Hibernate are portable
across databases with minimal changes.
• Performance Optimization – Features like caching and lazy loading help
optimize performance by reducing database access.
• Powerful Query Language – Hibernate Query Language (HQL) offers an
object-oriented extension to SQL, easing data manipulation and retrieval.
• Automatic Schema Generation – Hibernate can generate database schemas
based on the object model, simplifying initial setup and migrations.

By – Rakesh Kumar
Hibernate Architecture
• SessionFactory – A thread-safe, immutable cache of compiled mappings for a single
database. SessionFactory is a heavyweight object, usually created during application
initialization and kept for later use.
• Session – A single threaded, short-lived object representing a conversation between the
application and the database. It acts a s a factory for transaction instances and holds a first-
level cache of retrieved data.
• Transaction – A unit of work with the database represents an abstraction of the application
from the underlying transaction implementation.
• ConnectionProvider – Manages the database connections needed by Hibernate sessions. It
abstracts the application from underlying connection management mechanisms.
• TransactionFactory –Creates transaction instances, hiding the underlying transaction
implementation details from the application.

By – Rakesh Kumar
Example – Student
package com.example;
import jakarta.persistence.*;
@Entity
@Table(name = "student")
public class Student {
@Id
private int rollno;
@Column(name="name")
private String name;
public Student()
{ }
public Student(int rollno,String name)
{
this.rollno=rollno;
this.name=name;
}
By – Rakesh Kumar
Example – Student
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

By – Rakesh Kumar
Example – Configuration
<?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="hibernate.connection.driver.class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/college</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- JDBC connection pool (use built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

By – Rakesh Kumar
Example – Configuration
<!-- Show SQL -->
<property name="show_sql">true</property>
<!-- Automatically create/drop tables -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.example.Student"></mapping>
</session-factory>
</hibernate-configuration>

By – Rakesh Kumar
Example – Main
package com.example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.List;

public class Main {


public static void main(String[] args) {
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();

// Create session
Session session = factory.openSession();

By – Rakesh Kumar
Example – Main
session.beginTransaction();
List<Student> std=session.createQuery("from Student",Student.class).getResultList();
for(Student s:std)
{
System.out.println(s.getRollno()+" "+s.getName());
}
session.close();
factory.close();
}
}

By – Rakesh Kumar

You might also like