I found some of my JPA-related spec notes and thought to send them to
the list. They probably don't make sense to anyone but me and I know
some of what is covered was yanked from the spec in the final version
(lots of changes happened in Feb/March), but for the possible benefit
of others, here they are. If they don't make sense to you, please
crack open the spec before asking questions.
***THESE ARE NOT GUARANTEED TO BE CORRECT***
Date: February 11, 2006 7:39:48 PM PST
EntityManager
- Interface implemented by the PersistenceProvider, possibly
proxied or wrapped by J2EE Container
- Associated with a Persistence Context
- Methods used to interact with Persistence Context
- Can create, remove, find, and query entities
- EJB 2.1 Perspective: Imagine all the entity home interfaces
smushed into one.
Persistence Context
- Concept
- Set of unique entity instances
- Two underlying lifecycle concepts:
- Transaction-scoped persistence contexts
- Extended persistence context
Persistence Unit
- Concept
- Defines the set of entities that can be managed by an entity
manager
- db mapping occurs at a persistence unit level
- must contain all classes required to manage enties and mappings
EntityManagerFactory
- Interface implemented by the PersistenceProvider, possibly
wrapped by J2EE Container
- one factory per persistence unit
container-managed entity manager
- each entity manager reference points to the persistence context
of the current transaction
- Aka "propagated" persistence context
- container manages persistence context lifecycle:
- by Java EE container alone
- by Java EE container cooperating with pluggable Persistence
Provider
- Two underlying Persistence Context lifecycle concepts:
- Transaction-scoped persistence contexts
- With TX:
- Persistence Context created upon first EntityManager
request in JTA transaction
- Persistence Context destroyed on JTA transaction
commit/rollback
- No TX:
- Persistence Context created and destroyed each method
call
- Extended persistence context
-
- Scoped at the transaction level
- a give persistence unit will have one persistence context scoped
at the transaction level
- all entity manager instances will point to the same persistence
context
- entity manager references provided by container through:
- injection (@PersistenceContext)
- JNDI
- EntityManagerFactory.getEntityManager()
- entity manager factory references provided by container through:
- injection (@PersistenceUnit)
- JNDI
- Transactions controlled through JTA by container
- Aka "JTA entity manager"
application-managed entity manager
- each entity manager reference points to a unique persistence
context
- Aka "stand-alone" persistence context
- application manages persistence context lifecycle
(entityManager.isOpen, entityManager.close)
- entity manager references created through:
- entityManagerFactory.createEntityManager()
- entity manager factory references created through static
bootstrap call:
- javax.persistence.Persistence.createEntityManagerFactory
("PersistenceUnitFoo");
- Transactions controlled through EntityTransaction API by
application
- Aka "resource-local entity manager"
Web Containers and EJB Containers must support:
- container-managed entity managers (and propagated persistence
context)
- application-managed entity managers (and stand-along persistence
context)
Non J2EE environments must support:
- application-managed entity managers (and stand-along persistence
context)
- Optionally: container-managed entity managers (and propagated
persistence context)
public interface javax.persistence.EntityManagerFactory {
EntityManager createEntityManager();
EntityManager createEntityManager(PersistenceContextType type);
EntityManager getEntityManager();
void close();
public boolean isOpen();
}
public interface EntityManager {
public void persist(Object entity);
public <T> T merge(T entity);
public void remove(Object entity);
public <T> T find(Class<T> entityClass, Object primaryKey);
public <T> T getReference(Class<T> entityClass, Object primaryKey);
public void flush();
public void setFlushMode(FlushModeType flushMode);
public FlushModeType getFlushMode();
public void lock(Object entity, LockModeType lockMode);
public void refresh(Object entity);
public void clear();
public boolean contains(Object entity);
public Query createQuery(String ejbqlString);
public Query createNamedQuery(String name);
public Query createNativeQuery(String sqlString);
public Query createNativeQuery(String sqlString, Class resultClass);
public Query createNativeQuery(String sqlString, String
resultSetMapping);
public void close();
public boolean isOpen();
public EntityTransaction getTransaction();
}
public interface EntityTransaction {
public void begin();
public void commit();
public void rollback();
public boolean isActive();
}