Most of notes here coming from the book [ Apress Pro EJB 3 JPI]
http://developers.sun.com/learning/javaoneonline/2006/coreenterprise/TS-9056.pdf ?
Persistent Unit
Persistent context
Each persistent context is associated with a persisent unit.
transient vs @Transient
One to One Mapping - unidirection
One to One Mapping - bidirection
One to One mapping - same ID
bidirection
Transaction Manager
transaction-scoped
use @PersistentContext(unitName=....), stateless
extended
If persistent context passed to another EJB call, they can share context, otherwise, can not shared context initialized from caller.
transaction managment types
transaction synchronization
the process by which presistent context is registed with transaction so that persistence context is notified when transaction commit.
transaction association
act of binding context to a transaction
tranaction propagation
sharing persistence context between multi-container-managed entity manager in a single transaction
Persistence Context Collision
when a statelss bean call a method in stateful bean, container will check if there is active persistence context there, if so, it must be the same as the extended persistence context.
In this case, it finds the one passed from stateless bean, ERROR...
Exception - when a stateful session bean with extended persistence context create another stateful session bean that also use extended persistence context, the child will inherit parent's context
Differernce application-managed and container managed entity manager
Entity manager will joinTransaction if the persistence context was created earlier, NOT created within a transaction.
Good practice, never use two persistence context in one transaction, when using application-managed transaction
EntityTransaction is to imitate UserTransaction, difference is that EntityTransaction is implemented in term of transaction method on JDBC Connection interface.
Two things happen if transaction rolls back
As Java memory model is not transactional , after transaction failed, there might be some changes to entity instances, we need pay attention to the case:
Thing to note when removing a entity
If the entity to be removed is target of foreign key in another table, the foreign key must be cleared.
Cascade usage
There must be a clear parent-child relationship.
remove only happen at parent side
cascade delete will remove the entity who has relationship with others, so Becareful
flush event
1. if related entity is not managed, it can be new, or relationship where entity to be persisted has foreign key to the detached entity.