0% found this document useful (0 votes)
32 views22 pages

A RHS Hibernate

This document provides an overview of Hibernate, including: 1. Hibernate is an open-source object-relational mapping tool that makes data persistent by storing objects in a database. 2. Hibernate addresses the mismatch between object-oriented programming languages and relational databases through object-relational mapping. 3. Hibernate supports many databases, provides an object-oriented query language called HQL, and handles common data persistence tasks such as transaction management.
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)
32 views22 pages

A RHS Hibernate

This document provides an overview of Hibernate, including: 1. Hibernate is an open-source object-relational mapping tool that makes data persistent by storing objects in a database. 2. Hibernate addresses the mismatch between object-oriented programming languages and relational databases through object-relational mapping. 3. Hibernate supports many databases, provides an object-oriented query language called HQL, and handles common data persistence tasks such as transaction management.
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/ 22

BHAGVAN MAHAVIR

COLLAGE OF
ENGINEERING AND
TECHNOLOGY

TOPIC NAME: HIBERNATE

MADE BY:
1. KHUSHBU GAJERA(160060107011)
2. ANKITA PANDEY (160060107037)
3. RAHUL SALIYA (160060107051)
4. SNEHA PATEL (140060107044)
Table of Contents

1 What is Hibernate

2 Hibernate Architecture

3 Hibernate O/R Mapping

4 Difference Between HQL And SQL


What is Hibernate ?
• It is an Open source Object relational mapping
tool(ORM).
• Responsible for making data persistent by storing it in
database.

Object Relational Mapping


• It is a programming technique for converting object
type data of an object oriented programming language
into database tables.
• In simple words ORM means storing object data directly
to RDBMS tables.
Persistence
• Persistence simply means that we would like our
application’s data to outlive the applications process.
In Java terms, we would like the state of (some of) our
objects to live beyond the scope of the JVM so that the
same state is available later.

• Hibernate not only takes care of the mapping from Java


classes to database tables (and from Java data types to
SQL data types), but also provides data query and
retrieval facilities.
Why Object Relational Mapping (ORM)?
• When we work with an object-oriented system, there is
a mismatch between the object model and the
relational database. RDBMSs represent data in a tabular
format whereas object-oriented languages, such as
Java or C# represent it as an interconnected graph of
objects.
Consider Following Example
public class Employee { create table EMPLOYEE (
private int id; id INT NOT NULL auto_increment,
private String first_name; first_name VARCHAR(20) default NULL,
private String last_name; last_name VARCHAR(20) default NULL,
private int salary; salary INT default NULL,
PRIMARY KEY (id)
public Employee() {} );
public Employee(String fname, String lname, int salary) {
this.first_name = fname;
this.last_name = lname;
this.salary = salary;
}

public int getId() {


return id;
}

public String getFirstName() {


return first_name;
}

public String getLastName() {


return last_name;
}

public int getSalary() {


return salary;
}
}
JAVA CLASS RDBMS TABLE
First problem, what if we need to modify the design of our database
after having developed a few pages or our application? Second, loading
and storing objects in a relational database exposes us to the following
five mismatch problems

Granularity
• Sometimes you will have an object model, which has more classes
than the number of corresponding tables in the database.

Inheritance
• RDBMSs do not define anything similar to Inheritance, which is a
natural paradigm in object-oriented programming languages.

Identity
• An RDBMS defines exactly one notion of 'sameness': the primary key.
Java, however, defines both object identity (a==b) and object equality
(a.equals(b)).
Associations
Object-oriented languages represent associations using object references
whereas an RDBMS represents an association as a foreign key column.

Navigation
The ways you access objects in Java and in RDBMS are fundamentally
different.

The Object-Relational Mapping (ORM) is the solution to handle


all the above impedance mismatches.

Tier Overview
Browser Business Database
or App Presentation Database
logic connection

User JSP Java Hibernate


Hibernate Features
• O-R mapping using ordinary JavaBeans
• Hibernate takes care of mapping Java classes to database
tables using XML files and without writing any line of code.
• Hides details of SQL queries from OO logic.
• Provides simple APIs for storing and retrieving Java objects
directly to and from the database.
• If there is change in the database or in any table, then you
need to change the XML file properties only.
• Transaction management with rollback
• Provides simple querying of data.
• Fast development of application.
Supported Databases
Hibernate supports almost all the major RDBMS. Following is
a list of few of the database engines supported by Hibernate:

• HSQL Database Engine


• DB2/NT
• MySQL
• PostgreSQL
• FrontBase
• Oracle
• Microsoft SQL Server Database
• Sybase SQL Server
• Informix Dynamic Server
Hibernate Architecture
Hibernate has a layered architecture which helps the user to
operate without having to know the underlying APIs. Hibernate
makes use of the database and configuration data to provide
persistence services (and persistent objects) to the application.
Following is a very high level view of the Hibernate Application
Architecture.
Hibernate Architecture
• The Hibernate architecture includes many objects such
as persistent object, session factory, transaction
factory, connection factory, session, transaction etc.
• he Hibernate architecture is categorized in four layers.

1) Java application layer


2) Hibernate framework layer
3) Backhand API layer
4) Database layer
Following is a detailed view of the Hibernate Application
Architecture with its important core classes.

JDBC Java Database Connectivity


JTA Java Transaction API
JNDI Java Naming and Directory Interface
Configuration Object
The Configuration object is the first Hibernate object you
create in any Hibernate application. It is usually created
only once during application initialization. It represents a
configuration or properties file required by the Hibernate.

SessionFactory Object
Configuration object is used to create a SessionFactory object
which in turn configures Hibernate for the application using the
supplied configuration file and allows for a Session object to be
instantiated. You would need one SessionFactory object per
database using a separate configuration file. So, if you are using
multiple databases, then you would have to create multiple
SessionFactory objects.
Session Object
A Session is used to get a physical connection with a
database. The Session object is lightweight and designed to
be instantiated each time an interaction is needed with the
database. Persistent objects are saved and retrieved
through a Session object.

Transaction Object
A Transaction represents a unit of work with the
database and most of the RDBMS supports
transaction functionality. Transactions in Hibernate
are handled by an underlying transaction manager
and transaction (from JDBC or JTA). This is an
optional object
Query Object
Query objects use SQL or Hibernate Query Language (HQL)
string to retrieve data from the database and create
objects. A Query instance is used to bind query parameters,
limit the number of results returned by the query, and
finally to execute the query.
Hibernate Configuration
Hibernate requires to know in advance — where to find the
mapping information that defines how your Java classes relate
to the database tables. Hibernate also requires a set of
configuration settings related to database and other related
parameters. All such information is usually supplied as a
standard Java properties file called hibernate.properties, or
as an XML file named hibernate.cfg.xml.
hibernate.cfg.xml
<?xml version = "1.0" encoding = "utf-8"?> Prerequisites
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> • testdb database
<hibernate-configuration> • test user
<session-factory>
<property name = "hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name = "hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name = "hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name = "hibernate.connection.username">
root
</property
<property name = "hibernate.connection.password">
root123
</property>
<!-- List of XML mapping files -->
<mapping resource = "Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate Query Language (HQL)
Hibernate Query Language (HQL) is an object-oriented query
language, similar to SQL, but instead of operating on tables
and columns, HQL works with persistent objects and their
properties. HQL queries are translated by Hibernate into
conventional SQL queries, which in turns perform action on
database.
Differences between SQL and HQL:
• SQL is based on a relational database model whereas HQL is a
combination of object-oriented programming with relational
database concepts.

• SQL manipulates data stored in tables and modifies its rows and
columns. HQL is concerned about objects and its properties.

• SQL is concerned about the relationship that exists between two


tables while HQL considers the relation between two objects.

• HQL is translated to SQL.


References:
1. https://www.javatpoint.com/hibernate-
architecture
2. https://en.wikipedia.org/wiki/Hibernate_(frame
work)
3. https://www.slideshare.net/guest11106b/hibern
ate-presentation?next_slideshow=1
4. http://www.differencebetween.net/technology/
software-technology/difference-between-sql-
and-hql/

1
THANK YOU

You might also like