0% found this document useful (0 votes)
7 views19 pages

Slot 21, 22-JPA

The document provides an overview of the Java Persistence API (JPA), detailing its objectives, architecture, and entity class basics. It highlights the benefits of using JPA for managing relational data in Java applications, including reduced manual SQL coding and improved integration with frameworks like Spring Boot. Additionally, it includes practical examples of configuring JPA with Hibernate and creating a web application demo.

Uploaded by

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

Slot 21, 22-JPA

The document provides an overview of the Java Persistence API (JPA), detailing its objectives, architecture, and entity class basics. It highlights the benefits of using JPA for managing relational data in Java applications, including reduced manual SQL coding and improved integration with frameworks like Spring Boot. Additionally, it includes practical examples of configuring JPA with Hibernate and creating a web application demo.

Uploaded by

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

JPA(Java Persistence API )

Objectives
 Introduction to the java persistence API
 JPA entity class basics
 JPA Architecture
 JPA - Obtaining a JPA Database Connection
 Configure JPA with Hibernate Example
 JPA Web Application demo

09/19/25 2/14
JPA - Introduction
 a Java EE/Jakarta EE specification for managing relational data in
Java.
 a standard for ORM (Object-Relational Mapping), which
automatically converts data between Java objects (POJOs) and
database tables.
 JPA allows developers to work with objects instead of SQL
statements, with the implementation typically called a persistence
provider.
 Popular implementations of JPA include: Hibernate (most
popular)EclipseLink (official RI of JPA)Apache OpenJPA

3
Hibernate ORM mapping

4
Why use JPA?
 Reduce manual JDBC code: No need to write manual
SQL for CRUD (Create, Read, Update, Delete).
 Easy to maintain, extend: Manage relationships
between entities using annotations instead of SQL.
 Good integration with Java EE, Spring Boot: Easy to
integrate with popular frameworks.
 Support cache and high performance: Has a cache
mechanism to help query faster.

5
JPA Entity Class Basics
 Defining a JPA Entity Class
 A JPA Entity Class is a Java class that is mapped to a table
in a relational database.
 Each object of this class corresponds to a row in the table.
 Requirements for Entity Classes
 Must have @Entity annotation → Marks this as an entity.
 Must have parameterless constructor → JPA requires to
create object from database.Must not declare final → So
that JPA can extend or proxy the object.
 If transmitted over network (detached object) → Must
implement Serializable.

09/19/25 6/14
JPA Entity Class Basics
 Persistent Fields and Properties in
Entity Classes
 Should be declared as private, protected or
package-private.Accessed via getter/setter.
 Persistent Fields
 Getter/setter methods can be used to map
data.
 JPA uses the JavaBeans naming
convention to map these methods.
09/19/25 7/14
JPA Entity Class Basics
 Persistent Properties
 Using @Entity, @Id, @GeneratedValue, and

@Column annotations for mapping between Java object to


database table columns.
 Primary Keys in Entities
 Use @Id to mark primary keys.

 Can use @GeneratedValue to generate values ​

automatically.
 Relationship Mapping:
 One-to-One (@OneToOne)
 One-to-Many (@OneToMany)
 Many-to-One (@ManyToOne)
 Many-to-Many (@ManyToMany)

09/19/25 8/14
JPA - Architecture
 Class Level Architecture
 JPA Class Relationships
 JPA Simple Example
 Usage of EntityManager,
EntityManagerFactory and
EntityTransaction

09/19/25 9/14
JPA - Architecture
 Class Level Architecture

09/19/25 10/14
JPA - Architecture
1. EntityManagerFactory - This is a factory class of EntityManager. It
creates and manages multiple EntityManager instances.
2. EntityManager - It is an Interface, it manages the persistence operations
on objects. It works like a factory for Query instance.
3. Entity - Entities are the persistence objects, stored as records in the
database.
4. EntityTransaction - It has one-to-one relationship with EntityManager.
For each EntityManager, operations are maintained
by EntityTransaction class.
5. Persistence - This class contains static methods to obtain
EntityManagerFactory instance.
6. Query - This interface is implemented by each JPA vendor to obtain
relational objects that meet the criteria.

09/19/25 11/14
JPA - Architecture
 JPA Class Relationships

09/19/25 12/14
JPA Simple Example

09/19/25 13/14
JPA - Obtaining a JPA
Database Connection
 An EntityManager represents a database connection
and enables database operations, with each HTTP
request in a web app typically using a separate
instance.
 The EntityManagerFactory efficiently creates multiple
EntityManager instances for a specific database.
 Database modifications require an active transaction,
managed by an EntityTransaction.
 Additionally, an EntityManager serves as a factory for
Query instances to execute database queries.

09/19/25 14/14
Definition of a persistence
unit in an XML file

09/19/25 15/14
Definition of a persistence
unit in an XML file
Suppose TestDB_JPA has Product table as
follows:
CREATE TABLE [dbo].[Product](
[id] [int] IDENTITY(1,1) PRIMARY KEY NOT
NULL,
[name] [nvarchar](255) NOT NULL,
[price] [decimal](10, 2) NOT NULL,
[description] [nvarchar](500) NULL)

09/19/25 16/14
Usage of EntityManager,
EntityManagerFactory and EntityTransaction

Note: Always use Transaction when performing CRUD to ensure data


integrity, if the query is read-only (SELECT) in Hibernate using EntityManager
or Session.

09/19/25 17/14
JPA Web Application demo
1. Create an java with Ant Web Project
2. Add library
3. Project Structure
4. SQL server Database Setup
5. Create a JPA entity - Product.java
6. Hibernate Java-based configuration
7. Create a ProductDAO.java
8. Create s ProductService.java
9. Create a ProductServlet.java
10. Creating Product Listing JSP Page - product-list.jsp
11. Creating Error JSP page
12. Deploying and Testing the Application Demo
Summary
 Java persistence API
 JPA entity class basics
 JPA Architecture
 JPA - Obtaining a JPA Database Connection
 Configure JPA with Hibernate Example
 JPA Web Application demo

09/19/25 19/14

You might also like