JAVA
JAVA
6 ...
7 ...
CONTENTS
1. Introduction to Java Persistence API
2. JPA Architecture
3. JPA Installation
4. ORM Components
5. Creating JPA Entities
6. Advanced Mappings
7. Java Persistence Query Language (JPQL)
8. Performing CRUD Operations Using the JDBC API
Java Persistent
API (JPA)
The Java Persistence API (JPA) is a Java specification that
simplifies the management of relational data in
applications. It provides a framework for object-relational
mapping (ORM), enabling developers to interact with
databases using Java objects instead of complex SQL
queries. JPA is a standard interface implemented by
popular frameworks like Hibernate and EclipseLink,
making it easier to persist, retrieve, and manage data in
an object-oriented way. Its primary goal is to reduce
boilerplate code and improve productivity while ensuring
compatibility with multiple database systems.
Key Features
Simplicity: Automates the process of database interaction, reducing boilerplate code.
Object-Oriented Approach: Work with objects instead of raw data (tables and columns).
Platform Independence: Works with multiple databases and is not tied to a specific
provider.
Standardization: A standard interface implemented by popular ORM frameworks like
Hibernate and EclipseLink.
Ease of Use: Reduces complexity compared to JDBC (Java Database Connectivity).
Productivity: Developers focus on business logic instead of database management.
Maintainability: Promotes clean and modular code.
Portability: Easily switch between databases without changing the code.
For example,
Imagine an e-commerce application that needs to store information about products,
customers, and orders. Using JPA, you can represent each table (e.g., Products) as a Java
class, making database operations more intuitive and object-oriented.
The JPA architecture consists of several
JPA
components that work together to handle
database interactions:
Architecture
1. Entity Classes
2. Entity Manager
3. Persistence Unit
4. Persistence Context
Entity Classes
Represent database tables as
Java classes.
Each instance of an entity
corresponds to a row in the
table.
Entity Manager
The core interface for
interacting with the persistence
context.
Responsible for managing entity
lifecycle (e.g., persist, merge,
remove).
Persistence Unit
Defines a set of entity classes that are managed
together and configuration details (e.g., database
connection).
Configured in the persistence.xml file.
Persistence Context
A runtime environment where entities are managed.
Ensures that entities are synchronized with the
database.
How the Architecture Works:
For Gradle:
Creating a JPA Entity
1. Common Annotations:
@PrePersist / @PreUpdate: Lifecycle hooks.
@Transient: Excludes a field from persistence.
2. Defining Relationships:
@OneToOne: One-to-one mapping.
@OneToMany & @ManyToOne: Parent-child relationships.
@ManyToMany: Many-to-many relationships.
Repository for Entity:
Interface for database operations:
Introduction to Java Persistent
Query Language (JPQL)
What is JPQL?
Key Features:
Why JPQL?