ocejpa study notes

20
Albert [email protected] OCEJPA Study Notes 1

Upload: guo-albert

Post on 19-May-2015

704 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: OCEJPA Study Notes

Albert

[email protected]

OCEJPA Study Notes

1

Page 2: OCEJPA Study Notes

Entity Relationship

2

Page 3: OCEJPA Study Notes

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

Page 4: OCEJPA Study Notes

@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

Page 5: OCEJPA Study Notes

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

Page 6: OCEJPA Study Notes

6

Page 7: OCEJPA Study Notes

Lock

7

Page 8: OCEJPA Study Notes

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

Page 9: OCEJPA Study Notes

Lock

9

Request for

Pessimistic lock

Tx-level rollback

Statement-level

rollback

PessimisticLockException LockTimeoutException

Page 10: OCEJPA Study Notes

Exception

10

Page 11: OCEJPA Study Notes

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

Page 12: OCEJPA Study Notes

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

Page 13: OCEJPA Study Notes

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

Page 14: OCEJPA Study Notes

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

Page 15: OCEJPA Study Notes

Exception

15

Persistence Exception

EntityExists Exception

EntityNotFound Exception

NonUniqueResult Exception

NoResult Exception

OptimisticLock Exception

Rollback Exception

TransactionRequired Exception

Page 16: OCEJPA Study Notes

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.

Page 17: OCEJPA Study Notes

Entity Manager

17

Page 18: OCEJPA Study Notes

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

Page 19: OCEJPA Study Notes

EntityManager’s clear() method

19

Can be called on any type of entity manager

Caused all managed entities to become detached

Page 20: OCEJPA Study Notes

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