Documente Academic
Documente Profesional
Documente Cultură
1 of 38
Objectives
2 of 38
What Is Persistence?
3 of 38
Persistence: Overview
Entity/JPA
Persistence
Architecture
4 of 38
Persistence Layer
5 of 38
What Are POJOs?
A POJO:
Stands for Plain Old Java Object
Does not implement any special interface
Is not concerned with “wiring” up to a framework
Enables developer to focus on business logic
Contains field definitions and accessors
6 of 38
EJB 3.0 - Java Persistence API
7 of 38
JPA Entities
8 of 38
Entity Annotations
@Entity
– @Id
– @IdClass
– @EmbeddedId
@Table, @SecondayTable
– @UniqueConstraint
– @PrimaryKeyJoinColumn(s)
@NamedQuery(s)
Sequencing
– @GeneratedValue
– @SequenceGenerator
– @TableGenerator
9 of 38
How Do JPA Entities Work?
A JPA entity:
Is a lightweight object that manages persistence data
Is defined as a Plain Old Java Object (POJO) marked with
the Entity annotation (no interfaces required)
Must implement the java.io.Serializable interface
to be passed by value to a remote application
Is mapped to a database by using annotations
@Entity
@Table(name=“PRODUCTS")
@Id
@Column(name=“PRODID")
POJO ORDERS
Database
10 of 38
Java Persistence API at Work
CreateProducts Product
object
Invoke addProducts
ProductsBean Product
entity
Invoke persist
EntityManager
Insert record
Products table
11 of 38
JPA O/R Mapping
12 of 38
Mapping Types
13 of 38
Specifying Object Relational (O/R) Mapping
@Entity // annotation
public class Products implements Serializable {
@Column(nullable = false)
private String description;
private String image;
@Column(nullable = false)
private String name;
@Id
@Column(name="PROD_ID", nullable = false)
private Long prodId;
… PRODUCTS
} PRODID (PK)
NAME
14 of 38
Mapping Relationships Between Entities
ServiceRequest ServiceHistory
OneToMany
Users Products
ManyToMany and
AssociationTable
15 of 38
Managing Persistence of JPA Entities
16 of 38
Entity Manager
Querying:
– Retrieval of entities from the database
Transactions:
– Applying changes to entities
General:
– isOpen()
– close()
– contains(Object entity)
Usage:
– Container managed (injection)
– Application bootstrap (EntityManagerFactory)
17 of 38
Entity Manager
Commit SQL
records statement
Retrieve Query
records results
18 of 38
JPA Entity Manager
19 of 38
Life Cycle of an Entity
Persisted
find()
new()
query()
Detached
20 of 38
Persistence Context and Scope
Transaction
Persistence Entity
Context
Detached
Entity
21 of 38
Manipulating Records with JPA
Persistence mapping
SELECT
Operates Relationship
UPDATE
on
Annotations, deployment
Compiled descriptor, or both
Database
22 of 38
Writing Basic JPA QL SELECT Statements
23 of 38
Creating JPA Entities
24 of 38
Summary
25 of 38