Java Persistence
Java Persistence
What is JPA ?
It is a specification that provides a standardized way to manage relational data in
Java applications through object-relational mapping (ORM).
It acts as a bridge between object-oriented domain models and relational
databases, allowing developers to define how Java objects are stored in and
retrieved from databases.
WHY
Allow developers to work with Java objects instead of SQL queries directly
JPA does not perform any operations on database, instead it uses hibernate or
eclipselink.
ENTITY:
entity is a lightweight, persistent domain object that represents a table in a
relational database.
ORM:
Object-Relational Mapping: Mapping of objects to Database. POJOs to Relational DB.
OOP approach.
Provide Abstraction
ex. Hibernate
JPA Architecture & its Components
i. Entity: lightweight, persistent domain object that represents a table in a
relational database.
ii. EntityManager:
Manages the Entity Lifecycle
Provides methods for querying and transaction control
iii. EntityManagerFactory:
Responsible for creating EntityManager instances.
It is created once per application and is thread-safe.
iv. Entity Transaction:
Interface is used to manage transactions in the
persistence context.
Methods : begin(), commit(), rollback()
adhere to ACID properties
v. PersistenceContext:
A context for managing entities and tracking changes.
Ensures consistency of entities during a transaction.
vi. Query:
Interface for executing queries
Supports parameter binding and dynamic query creation
vii. Persistence Unit:
Defined in persistence.xml, specifies database connection
and configuration.
Contains entity classes and JPA provider details.
viii. JPA Provider:
Implementation of the JPA specification (e.g., Hibernate,
EclipseLink).
Responsible for translating JPA calls into database
operations.
ENTITY LIFECYCLE:
1. TRANSIENT:
created but not yet associated with an EntityManager
no representation in the database.
2. PERSISTENT:
entity is managed by the EntityManager and is associated
with a persistence context.
Changes are automatically tracked and synchronized with the
database.
Will be persisted when the transaction is committed.
3. MERGE
reattach a detached entity to the persistence context.
4. DETACHED
entity that was previously persistent but is no longer
associated with the current persistence context.
5. REMOVE
entity that has been marked for deletion.
1. Transient to Managed:
Transition occurs when calling persist() on an EntityManager.
2. Managed to Detached:
Occurs when the EntityManager is closed or when you call detach().
3. Managed to Removed:
Happens when calling remove() on an EntityManager.
4. Detached back to Managed:
Can be done using merge() to reattach a detached entity.
RELATIONSHIPS
@JoinColumn: defining how entities are related through foreign keys in the
database.
@JoinTable:
1. A Join Table is essential for managing many-to-many relationships in JPA.
2. It allows for a clean and organized way to relate two entities without
directly modifying their tables.
3. Properly configuring the join table with @JoinTable and its attributes
ensures that the relationship is effectively maintained and data integrity is
upheld.
Derived Query: Spring Dat JPA generates queries based on method names.
1. findBy
2. countBy
3. deleteBy
PERSISTENCE UNIT (Used for Java Applications not for Spring Data JPA)
Configuration that defines a set of entity classes, the database connection, and
various properties for managing persistence in a Java application.
Persistence Unit consists of
1. persistence.xml
2. Entity classes
3. Data source
4. JPA Provider
5. Properties
SPRING DATA JPA
Part of the Spring Data project that simplifies data access in Java applications by
providing an abstraction over JPA (Java Persistence API). It aims to reduce
boilerplate code and streamline data access operations.
Features
1. Abstraction: CrudRepository, PagingAndSortingRepository, JpaRepository
2. Query Methods
3. Pagination and sorting
4. Integration with spring
Transaction: Transaction ensures that all operations either complete successfully or
none, adhering to ACID properties
@Transactional
@Entity
@Id
@Column
@Table
PAGING:
divide large datasets into smaller, manageable chunks
efficient data retrieval and improved performance by loading only a subset of
results at a time.
SORTING:
incorporate sorting into your pageable request
Sorting of data in ascending order
TRANSACTION LIFECYCLE
1. Start
2. Propagation
3. Business Logic
4. Commit or Rollback
5. End
LAZY vs EAGER LOADING
@OneToMany(fetch = FetchType.EAGER)
@OneToMany(fetch = FetchType.LAZY)
CASCADING TYPES
Cascading in JPA allows you to propagate operations (like persist, merge, remove)
from a parent entity to its associated child entities. This is particularly useful for
managing the lifecycle of related entities in a more convenient way.
@OneToMany(cascade = CascadeType.ALL)
CascadeType.PERSIST
CascadeType.MERGE
CascadeType.REMOVE
CascadeType.REFRESH
CascadeType.DETACH
1. Introduction to JPA (Java Persistence API)
o Overview of ORM (Object-Relational Mapping)
5. Transaction Management
o Understanding the transaction lifecycle