4. @JoinTable Apply to…
4
many-to-many and unidirectional one-to-many associations
(assign on owning side)
bidirectional many-to-one/one-to-many associations
unidirectional many-to-one relationships
one-to-one associations (both bidirectional and unidirectional)
A B
A B
A B
A B
A B
A B
A Bm n
1 n
n 1
1 n
n 1
1 1
1 1
5. Rule for Maps
Rules for Maps Description
Use the @MapKeyClass and targetEntity
/targetClass elements of the relationship
and element collection mappings
specify the classes when an untyped Map is used
Use @MapKey with one-to-many or
many-to-many relationship Map
keyed on an attribute of the target entity
Use @MapKeyJoinColumn override the join column of the entity key.
Use @Column override the column storing the values of an element
collection of basic types
Use @MapKeyColumn override the column storing the keys when keyed by
a basic type
Use @MapKeyTemporal and
@MapKeyEnumerated
if you need to further qualify a basic key that is a
temporal or enumerated type
Use @AttributeOverride with a “key.” or
“value.” prefix
override the column of an embeddable attribute type
that is a Map key or a value, respectively
5
8. Valid LockType
TYPE OPTIMISTIC Description
READ OPTIMISTIC Prevents dirty read and non-repeatable
read
WRITE OPTIMISTIC_FORECE_INCREMENT Prevents dirty read and non-repeatable
read as well as increments the version of
the object
TYPE PESSIMISTIC Description
READ PESSIMISTIC_READ Prevents dirty read and non-repeatable
read
WRITE PESSIMISTIC_WRITE Prevents dirty read and non-repeatable
read as well as increments the version of
the object
8
11. Rollback issue
If a system exception occurs within a transaction, the EJB container rolls back the transaction.
However, if an application exception is thrown within a transaction, the container does not
roll back the transaction.
ExceptionApplication
Exception
System Exception
• Checked
Exception
• Unchecked
Exception
EJB container doesn't roll back
the tx unless it is asked
to do so explicitly
EJB container rolls back the
Tx (if within tx)
11
12. EJBException or RemoteException
A system exception in bean’s method translates to an
EJBException for a local client and a RemoteException
for a remote client
System
Exception
EJB
Exception
Remote
Exception
To local client To remote client
12
13. JPA Exceptions
Exception Name ThrowWhen Rollback
NoResultException Query.getSingleResult or
TypedQuery.getSingleResult is invoked and
there is no result to return.
No
NonUniqueResultException Query.getSingleResult or
TypedQuery.getSingleResult is invoked and
there is more than one result from the query.
No
QueryTimeoutException A query times out and only the statement is rolled
back
No
LockTimeoutException A pessimistic locking conflict occurs that does not
result in transaction rollback.
Typically this occurs because the database uses
statement-level rollback when a pessimistic lock
cannot be granted (and there is no deadlock).
No
13
14. JPA Exceptions – cont.
Exception Name ThrowWhen Rollback
TransactionRequiredException Transaction is required but is not active Yes
OptimisticLockException An optimistic locking conflict occurs.
This exception may be thrown as part of anAPI call,
at flush, or at commit time.
Yes
PessimisticLockException A pessimistic locking conflict occurs. Yes
RollbackException EntityTransaction.commit is failed Yes
EntityExistsException The persist operation is invoked and the entity
already exists.
Yes
EntityNotFoundException An entity reference obtained by getReference is
accessed but the entity does not exist.
It is thrown by the refresh operation when the
entity no longer exists in the database.
It is also thrown by the lock operation when
pessimistic locking is used and the entity no longer
exists in the database.
Yes
14
18. Propagation of Persistence Context
18
Only applied for local
environment, not for
remote
Only for container
managed entity managers
Propagation support for all
kinds of bean
Eliminate the need to pass the
reference of EntityManager in
method call
20. When is tx scoped persistence context
created
Container Managed Entity
Manager
Bean Managed Entity
Manager
20
When the entity is invoked
in the scope of an active
JTA tx
As soon as an
EntityManager is created