ocejpa study notes
TRANSCRIPT
Entity Relationship
2
Entity Relationship Annotation Description
@CollectionTable 要與@ElementCollection一起使用。當Map value type是basic type
or embeddable class,適用於@CollectionTable。
@ElementCollection 若是Map<entity, basic type>,就一定要用@ElementCollection
(Map value是basic type或embedded class), 如Map<Customer,
String>
@OneToMany、@ManyToMany
若Map value是entity,則要用@OneToMany、@ManyToMany, 如Map<String, Customer>
@MapKeyJoinColumn 用於map key是用entity的狀況, 如Map<Customer, String>
@MapKeyColumn map key只能接受basic typeManyToMany, 如Map<String,
Customer>
@OrderColumn 只能用於@OneToMany與@ManyToMany,而且要與@ElementCollection一起用(與relationship中的owing side無關)
@MapKey與@MapKeyClass只能擇一使用
3
@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 B m n
1 n
n 1
1 n
n 1
1 1
1 1
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
6
Lock
7
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
Lock
9
Request for
Pessimistic lock
Tx-level rollback
Statement-level
rollback
PessimisticLockException LockTimeoutException
Exception
10
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.
Exception Application 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
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
JPA Exceptions Exception Name Throw When 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
JPA Exceptions – cont. Exception Name Throw When Rollback
TransactionRequiredException Transaction is required but is not active Yes
OptimisticLockException An optimistic locking conflict occurs.
This exception may be thrown as part of an API 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
Exception
15
Persistence Exception
EntityExists Exception
EntityNotFound Exception
NonUniqueResult Exception
NoResult Exception
OptimisticLock Exception
Rollback Exception
TransactionRequired Exception
Exception
16
EJBException
EJBTransactionRolledback Exception
TransactionRequiredLocal Exception
TransactionRolledbackLocal Exception
EJBTransactionRequired Exception
EJBException is kind of system exception. When a bean method encounters system exception,
the bean instance will be discarded, and the transaction will be marked for roll back.
Entity Manager
17
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
EntityManager’s clear() method
19
Can be called on any type of entity manager
Caused all managed entities to become detached
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