Introduction To Hibernate-JPA
Introduction To Hibernate-JPA
Topics
• Hibernate Introduction
• Benefits of Hibernate
Maintainability: Hibernate reduces boilerplate code, making system more understandable and
easier to refractor.
Performance: Hibernate provides the object-to-Relational mapping (ORM) automation and allows
multiple performance optimizations.
Vendor Independence: Hibernate can help mitigate risks associated with vendor lock-ins. This
enables a certain level of portability.
Object Relational
Mapping (ORM)
Object-To-Relational Mapping
• Object relational mapping (ORM) is a programming technique for converting data between an
object-oriented platform and a relational database platform.
• ORM allows easy transfer of data between RDBMS tables and the objects that are part of the
application code.
• The developer defines mapping between Java class and database table.
Problem with JDBC
Id: int save(ob
j) Selec id firstName
firstName:
t lastName
String
Insert
lastName: Updat
String get(id) e
Delet
Java e
Object
RDBMS
Manually Written Table
Code
□ Takes effort
□ Error-prone
□ Needs to be
tested
□ Needs to be
rewritten when
the database
Using ORM
Id: int save(ob
j) id firstName
firstNam
lastName
e: String OR
lastNam M
e: get(id)
String
Java
Object
RDBMS
External Library Table
(Jar)Takes no extra effort;
□
simply add as dependency
□ Extremely less chances of
bugs
□ Well-tested
□ Just mention which database
you are using; it will adapt
Using ORM
□ Tasks that require developers’ efforts while working with the plain JDBC API can be
done automatically by ORM.
● Eg: To fetch data from the RDBMS table, ORM uses the SELECT query of JDBC
API, gets the ResultSet and maps it to Collection objects.
□ Examples of Java ORM frameworks : Hibernate (for RDBMS) and Toplink (for
both RDBMS and non-RDBMS).
ORM and JDBC
y SQ
M Oracl
L ive e
Dr
r
Java ORM
JDBC MySQ
Applicati Tool
API L
on (Hibernat
e)
MS
SDQ
r iLv
e r MS
SQL
Java Persistence
API (JPA) and
Hibernate
What is a Java Specification?
□ Java provides JDBC API as a collection of interfaces that is implemented by the JDBC
Drivers.
● JDBC Drivers may name methods differently, which implies one has to learn how to
work with different drivers.
□ To work with a database, plug the driver while running the application =>JDBC API is a
Java Specification.
Java Specification
□ Specifications in Java define a set of interfaces, method declarations or
annotations provided by the Java technology as a standard set of rules.
□ JDBC API is a specification to connect Java applications with RDBMS, and MySQL
Connector/J is one of its implementations, which is a JDBC Driver.
• A language and APIs for specifying queries that refer to classes and properties of
classes
Using Java Persistence Query Language (JPQL) that looks similar to SQL
JPA - ORM
MySQ
L
Java ORM Drive
JPA JDBC MySQ
Applicati Tool r
API L
on (Hibernat
e)
Concrete
Specificati
Implementati
on
on
References
• Hibernate: Everything Data https://hibernate.org/orm/
• What is an ORM, how does it work? and how should I use one?
https://stackoverflow.com/questions/1279613/what-is-an-orm-how-d
oes-it-work-and-how-should-i-use-one/1279678#1279678