0% found this document useful (0 votes)
22 views18 pages

Introduction To Hibernate-JPA

The document provides an overview of Hibernate, a Java open-source framework designed to simplify CRUD operations and facilitate Object Relational Mapping (ORM). It highlights the benefits of Hibernate, such as increased productivity, maintainability, performance, and vendor independence, while also comparing it to the JDBC API. Additionally, it introduces the Java Persistence API (JPA) as a specification for ORM, emphasizing the importance of coding to specifications for better flexibility and reduced coupling in applications.

Uploaded by

jainabhina.aj
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)
22 views18 pages

Introduction To Hibernate-JPA

The document provides an overview of Hibernate, a Java open-source framework designed to simplify CRUD operations and facilitate Object Relational Mapping (ORM). It highlights the benefits of Hibernate, such as increased productivity, maintainability, performance, and vendor independence, while also comparing it to the JDBC API. Additionally, it introduces the Java Persistence API (JPA) as a specification for ORM, emphasizing the importance of coding to specifications for better flexibility and reduced coupling in applications.

Uploaded by

jainabhina.aj
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/ 18

Hibernate and JPA

Topics
• Hibernate Introduction

• Benefits of Hibernate

• Object Relational Mapping (ORM)

• Comparison with JDBC API

• Java Persistence API (JPA)


Hibernate
• A Java open-source framework for
simplifying the CRUD database operations

• A framework for persisting/saving Java


objects in database

• Build around 2001 by Gavin King as an


alternative to EJB2

• Official website: www.hibernate.org


Benefits of Hibernate
 Productivity: Hibernate eliminates much of the groundwork and lets you concentrate on the
business problem and reduces the development time.

 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.

□ ORM does not replace JDBC API but uses it internally.

● 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.

□ It supports primary key generation strategies based on the underlying RDBMS.

□ It also supports database neutral SQL queries to operate on RDBMS.

□ 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.

□ Without JDBC API, code is directly dependent on the JDBC Drivers,

● switching from one RDBMS to another is difficult, because of tight coupling.

● JDBC Drivers may name methods differently, which implies one has to learn how to
work with different drivers.

□ JDBC standardises everything while working with JDBC 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.

□ Vendor companies must provide code implementation according to specifications,


which ensures uniformity between different implementations.

□ Some examples of specifications are as follows:

● JDBC Specification: It is implemented by different RDBMS vendors such as Oracle


and MySQL.

● Servlet Specification: It is implemented by different Servlet containers such as


Tomcat and JBoss.
Java Persistence API (JPA)

□ Do not write code to implementations (concrete classes), but to specifications


(interfaces).

□ Are we coding to implementations while working with ORM tools?

□ What if we want to change the ORM tool in future?

□ Does Java provide a specification for ORM as well?

□ Yes, and that specification is called JPA (Java Persistence API).


Java Persistence API (JPA)

□ JPA is a specification for an ORM technique, and Hibernate is one of its


implementations, which is an ORM tool.

● No two concrete modules are tightly coupled with each other.

● Interaction with each other happens via a specification.

□ 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.

□ Write code to specifications (or interfaces) to make your application loosely


coupled.
Java Persistence API (JPA)
• The JPA specification defines the following:

• A facility for specifying mapping metadata—how persistent classes and their


properties relate to the database schema?
 Using Annotations (modern way) or xml (legacy way)

• APIs for performing basic CRUD operations on instances of persistent classes

• 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

• Introduction to Java Persistence API


https://docs.oracle.com/javaee/6/tutorial/doc/bnbpz.html

You might also like