2. www.luxoft.com
What is Java Persistence API ?
JPA - is the specification of the Java API for the management of
persistence and object/relational mapping
4. www.luxoft.com
How JPA specification has been created ?
Java Community Process (JCP) established since 1998
- Java Specification Request (JSR)
Specification: JSR 338: Java™ Persistence API, Version 2.1
Reference implementation: EclipseLink 2.5.0
Technology Compatibility Kit: test suite
6. www.luxoft.com
Entity
Starting point: object/relational mapping
- Annotations or XML descriptor
- Syntax - just Google it
- Java to DDL (vise versa) generation
Result: Java classes, database schema and
mapping meta information
7. www.luxoft.com
Entity Manager Factory
Configuring “persistance.xml” parameters:
- Required: Persistent-Unit name only
- Optional: DB connection parameters, transaction type, mapping
metadata (XML, root jar, list of Classes), persistency-provider properties
Result: Entity Manager Factory ready for persistence
Entity Manager
Factory
Connection and
mapping
metadata
8. www.luxoft.com
Entity Manager & Persistent Context
Entity manager
- Used by Developer. Manages Entity live-circle (CRUD), JPQL, etc.
- Created by Entity Manager Factory
- Interacts with transactions (create / join)
- Entity keep is delegated to Persistent Context
SQL Read on demand, but Persistent Context
SQL Write on “flush()” call
- Session scope, not thread-safe
Entity Manager
Persistence
Context
Entity
Entity Manager
Factory
Persistent Context
- Accessible by Entity Manager ‘s only
- L1 cache. Identity Set (search with ‘’==“)
- Produces REPETABLE_READ behavior
Transaction
9. www.luxoft.com
Entity Manager – create & delete operations
Search in
Persistence Context by
“==“
no equals()/hashCode()
required
11. www.luxoft.com
Entity Manager – update operation
Dirty check – depends on persistent-provider.
No equals()/hashCode() required.
E.g. Hibernate uses “inspection” strategy by default
12. www.luxoft.com
Entity Manager – transaction types
JPA supports two transaction types
- RESOURSE_LOCAL – transactions created and controlled
by Entity Manager (Java SE & Java EE).
- JTA – transactions created and controlled by Container
(Java EE, JSR-907). Default value.
Transaction type is defined in “percistance.xml”
- Entity Manager Factory creates Entity Managers
configured for one and only one transaction type
- Simultaneous work with two transaction types is not
supported within one Entity Manager Factory
Partly transparent for Developer
Entity Manager
Entity Manager
Factory
Resource-local
Entity Manager
JTA
Entity Manager