JPA Notes

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

  • A set of managed classes that are mapped to a single database and their mapping metadata.
  • An entity manager factory and all entity managers obtained from it
  • Is defined in the persistence.xml file
  • <persistence xmlns="..." version="1.0">
  • <persistence-unit name="HumanResource"> ... </persistence-unit>
  • </persistence>

  Persistent context

  • It represents a collection of entities managed by an EntityManager
  • For each persistent entity identity there is a unique entity instance in a given Persistence
    Context

Each persistent context is  associated with a persisent unit.

 

 

transient vs @Transient  

  • transient for java object
  • @ is for hibernate persistent

ONE TO ONE

One to One Mapping - unidirection

  • use @OneToOne, source: @joinColumn, if need dualDirection, target used @mappedBy(Source Object propertyName)
  • use @ManToOne

One to One Mapping - bidirection

  • use @OneToOne, @joinColumn + @mappedBy for both sides

One to One mapping - same ID

  • source side (having foreight key) use @OneToOne + @PrimaryKeyJoinColumn
  • target side use @OneToOne(mappedBy = "..."), as its NOT main side.

One To Many

bidirection

  • Many side use @ManyToOne,@JoinColumn
  • One Side is main side: @OneToMany(mappedBy="")
  • using map, must define @mapKey which is attribute of entity in collection

Many to Many

  • Both side has One-To-Many relationship.
  • Only way to map is using join-table
  • any side can be owner side, owned side must use @mappedBy
  • Owner side: use @JoinTable(JoinColumns, inverseJoinColumn)
  • Owned side: use @mappedBy
  • uniDirection : use @OneToMany, similar as @ManToMany, no @mappedBy defined in owned side

 

Transaction Manager

transaction-scoped

use @PersistentContext(unitName=....), stateless

  1. ansaction scoped - stateless, container manage for us, use @PersistentContext(unitName=....)
  2. stateless check transaction and persistence context everytime invoke method
  3. created by transaction during transaction and closed after

extended

  1. it will exist with stateful bean, use @PersistenceContext(unitName=".." type="PersistenceContextType.Extened") , untile the bean is removed
  2. limitation - must coexist with stateful bean

Transaction management -transaction attribute default to "Requried"

If persistent context passed to another EJB call, they can share context, otherwise, can not shared context initialized from caller.

 

transaction managment types

  1. resource-local transaction
  2. java transaction API

 

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

  • There is no limit to number of application-managed persistence context associated with transaction
  • Only one container-managed persistence context will be associated

 

Entity manager will joinTransaction if the persistence context was created earlier, NOT created within a transaction.

  • Using joinTransaction() to synchronize itself with current JTA 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

  1. database transaction is rolled back
  2. persistence context is cleared, detached from managed entity instance, if persistence is transaction scoped, its removed.

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:

  1. If there is new entity that uses automatic primary key generation, primary key might be assigned to entity, need clear primary key

  2. if entity use version field for lock purpose which is automaticly maintained by persistence provider, it might has incorrect value

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.

 

你可能感兴趣的:(Hibernate,bean,jdbc,ejb,jpa)