org.objectweb.easybeans.persistence
Class TxEntityManager

java.lang.Object
  extended by org.objectweb.easybeans.persistence.TxEntityManager
All Implemented Interfaces:
javax.persistence.EntityManager

public class TxEntityManager
extends java.lang.Object
implements javax.persistence.EntityManager

This class represents an EntityManager that will be used as a container managed transaction scoped persistence context. The lifetime of this context is a single transaction. When the transaction is committed or rollbacked, the persistence context ends.

Author:
Florent Benoit

Field Summary
private  TxEntityManagerHandler handler
          Handler of the manager.
 
Constructor Summary
TxEntityManager(TxEntityManagerHandler handler)
          Build a new entity manager which have TransactionScoped type.
 
Method Summary
 void clear()
          Clear the persistence context, causing all managed entities to become detached.
 void close()
          Close an application-managed EntityManager.
 boolean contains(java.lang.Object entity)
          Check if the instance belongs to the current persistence context.
 javax.persistence.Query createNamedQuery(java.lang.String name)
          Create an instance of Query for executing a named query (in EJB QL or native SQL).
 javax.persistence.Query createNativeQuery(java.lang.String sqlString)
          Create an instance of Query for executing a native SQL statement, e.g., for update or delete.
 javax.persistence.Query createNativeQuery(java.lang.String sqlString, java.lang.Class resultClass)
          Create an instance of Query for executing a native SQL query.
 javax.persistence.Query createNativeQuery(java.lang.String sqlString, java.lang.String resultSetMapping)
          Create an instance of Query for executing a native SQL query.
 javax.persistence.Query createQuery(java.lang.String ejbqlString)
          Create an instance of Query for executing an EJB QL statement.
<T> T
find(java.lang.Class<T> entityClass, java.lang.Object primaryKey)
          Find by primary key.
 void flush()
          Synchronize the persistence context to the underlying database.
 javax.persistence.EntityManager getCurrentEntityManager()
          Gets (or create) a new EntityManager for the current tx (if any).
 java.lang.Object getDelegate()
           
 javax.persistence.FlushModeType getFlushMode()
          Get the flush mode that applies to all objects contained in the persistence context.
<T> T
getReference(java.lang.Class<T> entityClass, java.lang.Object primaryKey)
          Get an instance, whose state may be lazily fetched.
 javax.persistence.EntityTransaction getTransaction()
          Return the resource-level transaction object.
 boolean isOpen()
          Determine whether the EntityManager is open.
 void joinTransaction()
          TODO: document this.
 void lock(java.lang.Object entity, javax.persistence.LockModeType lockMode)
          Set the lock mode for an entity object contained in the persistence context.
<T> T
merge(T entity)
          Merge the state of the given entity into the current persistence context.
 void persist(java.lang.Object entity)
          Make an instance managed and persistent.
 void refresh(java.lang.Object entity)
          Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
 void remove(java.lang.Object entity)
          Remove the entity instance.
 void setFlushMode(javax.persistence.FlushModeType flushMode)
          Set the flush mode that applies to all objects contained in the persistence context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

handler

private TxEntityManagerHandler handler
Handler of the manager. Do the switch for each TX.

Constructor Detail

TxEntityManager

public TxEntityManager(TxEntityManagerHandler handler)
Build a new entity manager which have TransactionScoped type.

Parameters:
handler - object managing the transaction's EntityManager.
Method Detail

getCurrentEntityManager

public javax.persistence.EntityManager getCurrentEntityManager()
Gets (or create) a new EntityManager for the current tx (if any).

Returns:
an entity manager.

persist

public void persist(java.lang.Object entity)
             throws java.lang.IllegalArgumentException,
                    TransactionRequiredException
Make an instance managed and persistent.

Specified by:
persist in interface javax.persistence.EntityManager
Parameters:
entity - entity bean.
Throws:
java.lang.IllegalArgumentException - if not an entity or entity is detached
TransactionRequiredException - if there is no transaction and the persistence context is of type PersistenceContextType.TRANSACTION

merge

public <T> T merge(T entity)
        throws java.lang.IllegalArgumentException,
               TransactionRequiredException
Merge the state of the given entity into the current persistence context.

Specified by:
merge in interface javax.persistence.EntityManager
Type Parameters:
T - entity object's class.
Parameters:
entity - entity bean
Returns:
the instance that the state was merged to
Throws:
java.lang.IllegalArgumentException - if instance is not an entity or is a removed entity
TransactionRequiredException - if there is no transaction and the persistence context is of type PersistenceContextType.TRANSACTION

remove

public void remove(java.lang.Object entity)
            throws java.lang.IllegalArgumentException,
                   TransactionRequiredException
Remove the entity instance.

Specified by:
remove in interface javax.persistence.EntityManager
Parameters:
entity - entity bean
Throws:
java.lang.IllegalArgumentException - if not an entity or if a detached entity
TransactionRequiredException - if there is no transaction and the persistence context is of type PersistenceContextType.TRANSACTION

find

public <T> T find(java.lang.Class<T> entityClass,
                  java.lang.Object primaryKey)
       throws java.lang.IllegalArgumentException
Find by primary key.

Specified by:
find in interface javax.persistence.EntityManager
Type Parameters:
T - entity object's class.
Parameters:
entityClass - the class of the entity
primaryKey - the primary key
Returns:
the found entity instance or null if the entity does not exist
Throws:
java.lang.IllegalArgumentException - if the first argument does not denote an entity type or the second argument is not a valid type for that entity?s primary key

getReference

public <T> T getReference(java.lang.Class<T> entityClass,
                          java.lang.Object primaryKey)
               throws java.lang.IllegalArgumentException,
                      javax.persistence.EntityNotFoundException
Get an instance, whose state may be lazily fetched. If the requested instance does not exist in the database, throws EntityNotFoundException when the instance state is first accessed. (The persistence provider runtime is permitted to throw the EntityNotFoundException when getReference is called.) The application should not expect that the instance state will be available upon detachment, unless it was accessed by the application while the entity manager was open.

Specified by:
getReference in interface javax.persistence.EntityManager
Type Parameters:
T - entity object's class.
Parameters:
entityClass - the class of the entity
primaryKey - the primary key
Returns:
the found entity instance
Throws:
java.lang.IllegalArgumentException - if the first argument does not denote an entity type or the second argument is not a valid type for that entity?s primary key
javax.persistence.EntityNotFoundException - if the entity state cannot be accessed

flush

public void flush()
           throws TransactionRequiredException,
                  javax.persistence.PersistenceException
Synchronize the persistence context to the underlying database.

Specified by:
flush in interface javax.persistence.EntityManager
Throws:
TransactionRequiredException - if there is no transaction
javax.persistence.PersistenceException - if the flush fails

setFlushMode

public void setFlushMode(javax.persistence.FlushModeType flushMode)
Set the flush mode that applies to all objects contained in the persistence context.

Specified by:
setFlushMode in interface javax.persistence.EntityManager
Parameters:
flushMode - the mode of flushing

getFlushMode

public javax.persistence.FlushModeType getFlushMode()
Get the flush mode that applies to all objects contained in the persistence context.

Specified by:
getFlushMode in interface javax.persistence.EntityManager
Returns:
flushMode

lock

public void lock(java.lang.Object entity,
                 javax.persistence.LockModeType lockMode)
          throws javax.persistence.PersistenceException,
                 java.lang.IllegalArgumentException,
                 TransactionRequiredException
Set the lock mode for an entity object contained in the persistence context.

Specified by:
lock in interface javax.persistence.EntityManager
Parameters:
entity - entity bean
lockMode - mode for locking
Throws:
javax.persistence.PersistenceException - if an unsupported lock call is made
java.lang.IllegalArgumentException - if the instance is not an entity or is a detached entity
TransactionRequiredException - if there is no transaction

refresh

public void refresh(java.lang.Object entity)
             throws java.lang.IllegalArgumentException,
                    TransactionRequiredException,
                    javax.persistence.EntityNotFoundException
Refresh the state of the instance from the database, overwriting changes made to the entity, if any.

Specified by:
refresh in interface javax.persistence.EntityManager
Parameters:
entity - entity bean
Throws:
java.lang.IllegalArgumentException - if not an entity or entity is not managed
TransactionRequiredException - if there is no transaction and the persistence context is of type PersistenceContextType.TRANSACTION
javax.persistence.EntityNotFoundException - if the entity no longer exists in the database

clear

public void clear()
Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted.

Specified by:
clear in interface javax.persistence.EntityManager

contains

public boolean contains(java.lang.Object entity)
                 throws java.lang.IllegalArgumentException
Check if the instance belongs to the current persistence context.

Specified by:
contains in interface javax.persistence.EntityManager
Parameters:
entity - the entity bean
Returns:
true/false
Throws:
java.lang.IllegalArgumentException - if not an entity

createQuery

public javax.persistence.Query createQuery(java.lang.String ejbqlString)
                                    throws java.lang.IllegalArgumentException
Create an instance of Query for executing an EJB QL statement.

Specified by:
createQuery in interface javax.persistence.EntityManager
Parameters:
ejbqlString - an EJB QL query string
Returns:
the new query instance
Throws:
java.lang.IllegalArgumentException - if query string is not valid

createNamedQuery

public javax.persistence.Query createNamedQuery(java.lang.String name)
                                         throws java.lang.IllegalArgumentException
Create an instance of Query for executing a named query (in EJB QL or native SQL).

Specified by:
createNamedQuery in interface javax.persistence.EntityManager
Parameters:
name - the name of a query defined in metadata
Returns:
the new query instance
Throws:
java.lang.IllegalArgumentException - if a query has not been defined with the given name

createNativeQuery

public javax.persistence.Query createNativeQuery(java.lang.String sqlString)
Create an instance of Query for executing a native SQL statement, e.g., for update or delete.

Specified by:
createNativeQuery in interface javax.persistence.EntityManager
Parameters:
sqlString - a native SQL query string
Returns:
the new query instance

createNativeQuery

public javax.persistence.Query createNativeQuery(java.lang.String sqlString,
                                                 java.lang.Class resultClass)
Create an instance of Query for executing a native SQL query.

Specified by:
createNativeQuery in interface javax.persistence.EntityManager
Parameters:
sqlString - a native SQL query string
resultClass - the class of the resulting instance(s)
Returns:
the new query instance

createNativeQuery

public javax.persistence.Query createNativeQuery(java.lang.String sqlString,
                                                 java.lang.String resultSetMapping)
Create an instance of Query for executing a native SQL query.

Specified by:
createNativeQuery in interface javax.persistence.EntityManager
Parameters:
sqlString - a native SQL query string
resultSetMapping - the name of the result set mapping
Returns:
the new query instance

joinTransaction

public void joinTransaction()
TODO: document this.

Specified by:
joinTransaction in interface javax.persistence.EntityManager

getDelegate

public java.lang.Object getDelegate()
Specified by:
getDelegate in interface javax.persistence.EntityManager
Returns:
TODO: document this.

close

public void close()
           throws java.lang.IllegalStateException
Close an application-managed EntityManager. After an EntityManager has been closed, all methods on the EntityManager instance will throw the IllegalStateException except for isOpen, which will return false. This method can only be called when the EntityManager is not associated with an active transaction.

Specified by:
close in interface javax.persistence.EntityManager
Throws:
java.lang.IllegalStateException - if the EntityManager is associated with an active transaction or if the EntityManager is container-managed.

isOpen

public boolean isOpen()
Determine whether the EntityManager is open.

Specified by:
isOpen in interface javax.persistence.EntityManager
Returns:
true until the EntityManager has been closed.

getTransaction

public javax.persistence.EntityTransaction getTransaction()
                                                   throws java.lang.IllegalStateException
Return the resource-level transaction object. The EntityTransaction instance may be used serially to begin and commit multiple transactions.

Specified by:
getTransaction in interface javax.persistence.EntityManager
Returns:
EntityTransaction instance
Throws:
java.lang.IllegalStateException - if invoked on a JTA EntityManager or an EntityManager that has been closed.