14 - EJB and JPA Integration
14 - EJB and JPA Integration
Persistence Contexts
database tables
persistence.xml example
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" ...>
<persistence-unit
name="JPAEmbeddablesPU"
transaction-type="JTA">
<jta-data-source>
studentuDBDataSource
</jta-data-source>
</persistence-unit>
</persistence>
EJB - JPA integration
!!!
Persistence Context
(cache) contains only
managed entities
!!!
EJB - JPA integration
database automatically
commit
Changes in database (performed by some other application)
are not brought back to entity database is not being
monitored for changes
Manual synchronization
if entity1 is managed
data of entity1 is written to DB (at the end of transaction)
entity1 is returned as result
if entity1 is not managed (is detached)
entity1 is cloned
the clone is made managed (entity1 remains detached!)
clones data is written to DB (at the end of transaction)
clone is returned as the result of merge() operation
EJB - JPA integration
closed too
10
11
specification
when is closed
@PersistenceContext(
type=PersistenceContextType.TRANSACTION)
@PersistenceContext(
type=PersistenceContextType.EXTENDED)
12
@Stateless
Example
public class Bank {
@PersistenceContext private EntityManager em;
13
Example 1
14
@Stateless
public class Bean {
Notes for example
// this is transactional PersistenceContext:
@PersistenceContext private EntityManager em;
15
@Stateful
public class Bank {
@PersistenceContext(type=EXTENDED)
private EntityManager manager;
Example 2
16
Example 2
17
@Stateful
Notes for example 2
public class Bank {
@PersistenceContext(type=PersistenceContextType.EXTENDED)
private EntityManager manager;
// EntityManager is created when session bean is created,
// and is closed when session bean is destroyed
public void method1() {
1. Transaction begins
2. EntityManager is used to work with entities ...
3. Leaving the method, the transaction ends. Just before
transaction commit, all the changes are sent to database,
EntityManager remains alive. All entities remain managed.
}
public void method2() {
The same
}
// changes, made to entities in between calls to method1 and
method2, are written to database at the end of transaction!
EJB - JPA integration
18
19
20
components
client calls the first component that calls another one and so on
of them contain:
@PersistenceContext EntityManager em;
EntityManagers
21
Other limitations
From JPA 2.0 specification:
It is the responsibility of the application to insure that an
instance is managed in only a single persistence context.
22
Possible solutions
The first component in a use case (use case controller)
EntityManager cumbersome
mechanism instead:
1.
2.
23
24
R
Client
Comp1
RN
Comp2
R
Comp3
Comp5
RN
Comp4
Comp6
25
inherited by the second stateful session bean and bound to it, and
this rule recursively appliesindependently of whether
transactions are active or not at the point of the creation of the
stateful session beans.
If the stateful session beans differ in declared synchronization type,
the EJBException is thrown by the container.
26
, propagates
, error
, propagates
, inherits
, error
27
28
type SynchronizationType.SYNCHRONIZED.
29
, propagates
, propagates
, propagates
, error
30
Summary
EJB and JPA are well integrated
automatic transactions, automatic cache life-cycle
management, persistence context (cache) propagation
Without EJB things would be much more difficult:
manual transactions
manual EntityManager creation and closing
EntityManagers would have to be passed as parameters
to other components implementing the same use-case
manual thread management: programmer would have to
ensure that two threads are not using the same
EntityManager
EJB - JPA integration
31
Summary
We get all this for free just by writing these lines:
...
EJB - JPA integration
32
Controller
Declarative page flow
Declarative form validation
Page composition
Form resubmition prevention
Inversion of Control (IoC)
Dependency Injection
Action-based controller
Component-oriented
controller
Request, session contexts
Conversation context
Data Tier
Object/Relational Mapping (ORM)
CRUD SQL generation
Many-to-many relationships
Primary key generation
Lazy resolution of queries
n+1 select problem
Inheritance/polymorphic queries
Query caching
Disconnected operation mode
...
RDBMS vendor independence
33