Introduction To Java Persistence API
Introduction To Java Persistence API
1
Java Persistence API
2
Primary Features
3
Entities
4
Example
@Entity public class Customer implements Serializable {
@Id protected Long id;
protected String name;
@Embedded
protected Address address;
protected PreferredStatus status;
@Transient
protected int orderCount;
public Customer(){}
public Long getId(){return id;} protected void setId(Long id){this.id =id;}
public String getName(){return name;}
public void setName(String name){
this.name =name;
}
…
}
5
Entity Identity
6
Persistence context
7
Types of Entity Managers
• Container-managed
A typical JTA transaction involves calls across multiple components,
which in turn, may access he same persistence context
Hence, the persistence context has to be propagated with he JTA
transaction to avoid the need for the application to pass references to
EntityManager instances from one component to another
• Application-managed
Application manages the life time of he EnityManager
8
Two types of container-managed entity manager
9
Entity Lifecycle
• new
New entity instance is created
Entity is not yet managed or persistent
• persist
Entity becomes managed
Entity becomes persistent in database on Transaction commit
• remove
Entity is removed
Entity is dele ed from database on transaction commit
• refresh
Entity ’s state is reloaded from database
• merge
State of detached entity is merged back in o managed entity
10
Entity Relationships
11
Example
@Entity public class Customer {
@Id protected Long id;
…
@OneToMany protected Set<Order>orders =new HashSet();
@ManyToOne protected SalesRep rep;
…
public Set<Order>getOrders(){return orders;}
public SalesRep getSalesRep(){return rep;}
public void setSalesRep(SalesRep rep){this.rep =rep;}
}//
@Entity public class SalesRep {
@Id protected Long id;
…@OneToMany(mappedBy=“rep ”)
protected Set<Customer>customers =new HashSet();
…
public Set<Customer>getCustomers(){return customers;}
public void addCustomer(Customer customer){ getCustomers().add(customer);
customer.setSalesRep(this);
}}
12
Inheritance
13
Example
MappedSuperclass
@MappedSuperclass public class Person {
@Id protected Long id;
protected String name;
@Embedded protected Address address;
}
@Entity public class Customer extends Person {
@Transient protected int orderCount;
@OneToMany protected Set<Order>orders =new HashSet();
}
@Entity public class Employee extends Person {
@ManyToOne protected Department dept;
}
A mapped superclass cannot be a target of queries, and cannot be passed
to methods on EntityManager interface. It cannot be target of persistent
relationships.
14
Example
Abstract Entity
@Entity public abstract class Person {
@Id protected Long id;
protected String name;
@Embedded protected Address address;
}
@Entity public class Customer extends Person { @Transient protected
int orderCount;
@OneToMany protected Set<Order>orders =new HashSet();
}
@Entity public class Employee extends Person {
@ManyToOne protected Department dept;
}
An abstract entity can be a target of queries, and can be passed to
methods on EntityManager interface. It cannot be instantiated.
15
Persist
16
Cascading Persist
@Entity public class Customer {
@Id protected Long id; …
@OneToMany(cascade=PERSIST)
protected Set<Order>orders =new HashSet();
}
…
public Order addNewOrder(Customer customer, Product product){
Order order =new Order(product); customer.addOrder(order);
return order; }
Add Order into the underlying tab e at the time of adding Order to the
Customer entity's state.
17
Remove
18
Merge
19
Queries
20
Java Persistence Query Language
• An extension of EJB ™ QL
Like EJB QL, a SQL-like language
• Added functionality
Projection list (SELECT clause)
Explicit JOINS
Subqueries
GROUP BY,HAVING
EXISTS,ALL,SOME/ANY
UPDATE,DELETE opera ions
Additional functions
21
Projection
22
Subqueries
23
Joins
24
Update,Delete
25
Queries
• Static queries
Defined with Java language metada a or XML
• Annotations: @NamedQuery, @NamedNativeQuery
• Dynamic queries
Query string is specified a run time
• Use Java Persistence query language or SQL
• Named or positional parameters
• EntityManager is factory for Query objects
createNamedQuery,createQuery,createNativeQuery
• Query methods for con rolling max results, pagination,flush mode
26
Dynamic Queries
27
Static Query
28