org.apache.cayenne.jpa
Class ResourceLocalEntityManager

java.lang.Object
  extended by org.apache.cayenne.jpa.ResourceLocalEntityManager
All Implemented Interfaces:
CayenneEntityManager
Direct Known Subclasses:
JtaEntityManager

public class ResourceLocalEntityManager
extends Object
implements CayenneEntityManager


Field Summary
protected  ObjectContext context
           
protected  ResourceLocalEntityManagerFactory factory
           
protected  FlushModeType flushMode
           
protected  boolean open
           
protected  EntityTransaction transaction
           
 
Constructor Summary
ResourceLocalEntityManager(ObjectContext context, ResourceLocalEntityManagerFactory factory)
           
 
Method Summary
protected  void checkAttached(Object entity)
          Checks if an entity is attached to the current EntityManager, throwing IllegalArgumentException if not.
protected  void checkClosed()
          Throws an exception if called on closed factory.
protected  void checkNotRemoved(Object entity)
          Checks if an entity is not removed in the current EntityManager, throwing IllegalArgumentException if it is.
 void clear()
          Clear the persistence context, causing all managed entities to become detached.
 void close()
          Close an application-managed EntityManager.
 boolean contains(Object entity)
          Check if the instance belongs to the current persistence context.
 Query createNamedQuery(String name)
          Create an instance of Query for executing a named query (in EJB QL or native SQL).
 Query createNativeQuery(String sqlString)
          Create an instance of Query for executing a native SQL statement, e.g., for update or delete.
 Query createNativeQuery(String sqlString, Class resultClass)
           
 Query createNativeQuery(String sqlString, String resultSetMapping)
          Create an instance of Query for executing a native SQL query.
 Query createQuery(String ejbqlString)
          Create an instance of Query for executing an EJB QL statement.
<T> T
find(Class<T> entityClass, Object primaryKey)
          Find by primary key.
 void flush()
          Synchronize the persistence context to the underlying database.
 DataChannel getChannel()
          Returns a DataChannel of the peer ObjectContext.
 Object getDelegate()
           
protected  ResourceLocalEntityManagerFactory getFactory()
          Returns parent EntityManagerFactory.
 FlushModeType getFlushMode()
          Get the flush mode that applies to all objects contained in the persistence context.
<T> T
getReference(Class<T> entityClass, Object primaryKey)
          Get an instance, whose state may be lazily fetched.
 EntityTransaction getTransaction()
          Return the resource-level transaction object.
 boolean isOpen()
           
 void joinTransaction()
          Indicates to the EntityManager that a JTA transaction is active.
 void lock(Object entity, LockModeType lockMode)
           
<T> T
merge(T entity)
          Merge the state of the given entity into the current persistence context.
 void persist(Object entity)
          Make an instance managed and persistent.
 void refresh(Object entity)
          Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
 void remove(Object entity)
          Remove the entity instance.
 void setFlushMode(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

transaction

protected EntityTransaction transaction

factory

protected ResourceLocalEntityManagerFactory factory

flushMode

protected FlushModeType flushMode

open

protected boolean open

context

protected ObjectContext context
Constructor Detail

ResourceLocalEntityManager

public ResourceLocalEntityManager(ObjectContext context,
                                  ResourceLocalEntityManagerFactory factory)
Method Detail

getChannel

public DataChannel getChannel()
Returns a DataChannel of the peer ObjectContext.

Specified by:
getChannel in interface CayenneEntityManager

getFactory

protected ResourceLocalEntityManagerFactory getFactory()
Returns parent EntityManagerFactory.


close

public void close()
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.

Throws:
IllegalStateException - if the EntityManager is associated with an active transaction or if the EntityManager is container-managed.

isOpen

public boolean isOpen()

getDelegate

public Object getDelegate()

persist

public void persist(Object entity)
Make an instance managed and persistent.

Parameters:
entity - an object to be made persistent
Throws:
IllegalArgumentException - if not an entity.

merge

public <T> T merge(T entity)
Merge the state of the given entity into the current persistence context. Cayenne: Is this like localObject(s)?

Parameters:
entity -
Returns:
the instance that the state was merged to
Throws:
IllegalArgumentException - if instance is not an entity or is a removed entity

remove

public void remove(Object entity)
Remove the entity instance.

Parameters:
entity -
Throws:
IllegalArgumentException - if not an entity or if a detached entity.

find

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

Parameters:
entityClass -
primaryKey -
Returns:
the found entity instance or null if the entity does not exist
Throws:
IllegalArgumentException - if the first argument does not denote an entity type or the second argument is not a valid type for that

getReference

public <T> T getReference(Class<T> entityClass,
                          Object primaryKey)
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.

Parameters:
entityClass -
primaryKey -
Returns:
the found entity instance
Throws:
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
EntityNotFoundException - if the entity state cannot be accessed

flush

public void flush()
Synchronize the persistence context to the underlying database.

Throws:
PersistenceException - if the flush fails

setFlushMode

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

Parameters:
flushMode -

getFlushMode

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

Returns:
flushMode

refresh

public void refresh(Object entity)
Refresh the state of the instance from the database, overwriting changes made to the entity, if any.

Parameters:
entity -
Throws:
IllegalArgumentException - if not an entity or entity is not managed
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.


contains

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

Throws:
IllegalArgumentException - if not an entity

createQuery

public Query createQuery(String ejbqlString)
Create an instance of Query for executing an EJB QL statement.

Parameters:
ejbqlString - an EJB QL query string
Returns:
the new query instance
Throws:
IllegalArgumentException - if query string is not valid

createNamedQuery

public Query createNamedQuery(String name)
Create an instance of Query for executing a named query (in EJB QL or native SQL).

Parameters:
name - the name of a query defined in metadata
Returns:
the new query instance
Throws:
IllegalArgumentException - if a query has not been defined with the given name

createNativeQuery

public Query createNativeQuery(String sqlString,
                               Class resultClass)

createNativeQuery

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

Parameters:
sqlString - a native SQL query string
Returns:
the new query instance

createNativeQuery

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

Parameters:
sqlString - a native SQL query string
resultSetMapping - the name of the result set mapping
Returns:
the new query instance

joinTransaction

public void joinTransaction()
Indicates to the EntityManager that a JTA transaction is active. This method should be called on a JTA application managed EntityManager that was created outside the scope of the active transaction to associate it with the current JTA transaction.

This implementation throws a JpaProviderException, as it only supports resource-local operation.

Throws:
JpaProviderException - as this impementation only supports resource-local operation.

lock

public void lock(Object entity,
                 LockModeType lockMode)

getTransaction

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

Returns:
EntityTransaction instance

checkAttached

protected void checkAttached(Object entity)
                      throws IllegalArgumentException
Checks if an entity is attached to the current EntityManager, throwing IllegalArgumentException if not.

Throws:
IllegalArgumentException

checkNotRemoved

protected void checkNotRemoved(Object entity)
                        throws IllegalArgumentException
Checks if an entity is not removed in the current EntityManager, throwing IllegalArgumentException if it is.

Throws:
IllegalArgumentException

checkClosed

protected void checkClosed()
                    throws IllegalStateException
Throws an exception if called on closed factory.

Throws:
IllegalStateException


Copyright © 2001-2007 Apache Cayenne. All Rights Reserved.