0% found this document useful (0 votes)
3 views

Java Persistence

JPA Mockito ata resume

Uploaded by

Abs Wps
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Persistence

JPA Mockito ata resume

Uploaded by

Abs Wps
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

1. One-To-One: User has one profile. @OneToOne & @JoinColumn


2. One-To-Many: User has Multiple Posts. @OneToMany & @JoinColumn
3. Many-To-One: Multiple Posts have a single user. @ManyToOne &
@JoinColumn
4. Many-To-Many: Quiz Questions. @ManyToMany & @JoinTable
Unidirectional Relationship: one entity knows about the other, but not vice versa.
Only one side maintains the relationship.
When to use it:
1. Simple relationship
2. High performance
3. Read-only-relationship

Bi-Directional Relationship: both entities are aware of each other, allowing


navigation in both directions.
When to use it:
1. Full navigation
2. Complex Relationship
3. Consistency
4. Integrity
MAPPEDBY
Use mappedBy in the inverse side;
Inverse Side: Use mappedBy on the entity that does not own the relationship to
indicate that it references the owning entity.
Simplifies Relationships: Helps to manage complex relationships without duplicating
foreign keys or relationships.
Navigation: You can navigate in both directions but only update the relationship on
the owning side.

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

JPQL (Java Persistence Query Language)


1. Helps perform DB operations more object oriented way.
2. JPQL queries are based on entity names and their relationships rather than
table names and column names.
3. Similar to SQL just uses entity names and attributes
@Query: Allows custom JPQL & SQL queries in repository methods. Use for
complex queries.
nativeQuery=true for sql

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)

o JPA architecture and its components

o Understanding persistence contexts and entity managers

2. Entities and Annotations


o Defining entities with @Entity

o Using @Table, @Id, @GeneratedValue, and @Column

o Lifecycle of an entity (managed, detached, persistent, transient)

3. Relationships Between Entities


o One-to-One, One-to-Many, Many-to-One, Many-to-Many relationships

o Mapping relationships with annotations like @JoinColumn, @JoinTable,


and cascade types
4. Querying Data
o JPQL (Java Persistence Query Language) vs. Criteria API

o Using @Query and query derivation in Spring Data JPA

o Pagination and sorting with Spring Data repositories

5. Transaction Management
o Understanding the transaction lifecycle

o Configuring transactions with @Transactional

o Propagation and isolation levels


6. Performance Tuning
o Understanding fetching strategies (EAGER vs. LAZY loading)

o Caching with JPA (1st level vs. 2nd level caching)

You might also like