EclipseLink 2.0.0_ 2.0.0.v20090626-r4569 API Reference

javax.persistence
Interface EntityManager

All Known Subinterfaces:
JpaEntityManager

public interface EntityManager

Interface used to interact with the persistence context.

Since:
Java Persistence 1.0

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 is a managed entity instance belonging to the current persistence context.
 Query createNamedQuery(java.lang.String name)
          Create an instance of Query for executing a named query (in the Java Persistence query language or in native SQL).
<T> TypedQuery<T>
createNamedQuery(java.lang.String name, java.lang.Class<T> resultClass)
          Create an instance of TypedQuery for executing a named query (in the Java Persistence query language or in native SQL).
 Query createNativeQuery(java.lang.String sqlString)
          Create an instance of Query for executing a native SQL statement, e.g., for update or delete.
 Query createNativeQuery(java.lang.String sqlString, java.lang.Class resultClass)
          Create an instance of Query for executing a native SQL query.
 Query createNativeQuery(java.lang.String sqlString, java.lang.String resultSetMapping)
          Create an instance of Query for executing a native SQL query.
<T> TypedQuery<T>
createQuery(CriteriaQuery<T> criteriaQuery)
          Create an instance of TypedQuery for executing a criteria query.
 Query createQuery(java.lang.String qlString)
          Create an instance of Query for executing a Java Persistence query language statement.
<T> TypedQuery<T>
createQuery(java.lang.String qlString, java.lang.Class<T> resultClass)
          Create an instance of TypedQuery for executing a Java Persistence query language statement.
 void detach(java.lang.Object entity)
          Remove the given entity from the persistence context, causing a managed entity to become detached.
<T> T
find(java.lang.Class<T> entityClass, java.lang.Object primaryKey)
          Find by primary key.
<T> T
find(java.lang.Class<T> entityClass, java.lang.Object primaryKey, LockModeType lockMode)
          Find by primary key and lock.
<T> T
find(java.lang.Class<T> entityClass, java.lang.Object primaryKey, LockModeType lockMode, java.util.Map<java.lang.String,java.lang.Object> properties)
          Find by primary key and lock, using the specified properties.
<T> T
find(java.lang.Class<T> entityClass, java.lang.Object primaryKey, java.util.Map<java.lang.String,java.lang.Object> properties)
          Find by primary key, using the specified properties.
 void flush()
          Synchronize the persistence context to the underlying database.
 java.lang.Object getDelegate()
          Return the underlying provider object for the EntityManager, if available.
 EntityManagerFactory getEntityManagerFactory()
          Return the entity manager factory for the entity manager.
 FlushModeType getFlushMode()
          Get the flush mode that applies to all objects contained in the persistence context.
 LockModeType getLockMode(java.lang.Object entity)
          Get the current lock mode for the entity instance.
 Metamodel getMetamodel()
          Return an instance of Metamodel interface for access to the metamodel of the persistence unit.
 java.util.Map<java.lang.String,java.lang.Object> getProperties()
          Get the properties and associated values that are in effect for the entity manager.
 QueryBuilder getQueryBuilder()
          Return an instance of QueryBuilder for the creation of CriteriaQuery objects.
<T> T
getReference(java.lang.Class<T> entityClass, java.lang.Object primaryKey)
          Get an instance, whose state may be lazily fetched.
 java.util.Set<java.lang.String> getSupportedProperties()
          Get the names of the properties that are supported for use with the entity manager.
 EntityTransaction getTransaction()
          Return the resource-level transaction object.
 boolean isOpen()
          Determine whether the EntityManager is open.
 void joinTransaction()
          Indicate to the EntityManager that a JTA transaction is active.
 void lock(java.lang.Object entity, LockModeType lockMode)
          Lock an entity instance that is contained in the persistence context with the specified lock mode type.
 void lock(java.lang.Object entity, LockModeType lockMode, java.util.Map<java.lang.String,java.lang.Object> properties)
          Lock an entity instance that is contained in the persistence context with the specified lock mode type and with specified properties.
<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 refresh(java.lang.Object entity, LockModeType lockMode)
          Refresh the state of the instance from the database, overwriting changes made to the entity, if any, and lock it with respect to given lock mode type.
 void refresh(java.lang.Object entity, LockModeType lockMode, java.util.Map<java.lang.String,java.lang.Object> properties)
          Refresh the state of the instance from the database, overwriting changes made to the entity, if any, and lock it with respect to given lock mode type and with specified properties.
 void refresh(java.lang.Object entity, java.util.Map<java.lang.String,java.lang.Object> properties)
          Refresh the state of the instance from the database, using the specified properties, and overwriting changes made to the entity, if any.
 void remove(java.lang.Object entity)
          Remove the entity instance.
 void setFlushMode(FlushModeType flushMode)
          Set the flush mode that applies to all objects contained in the persistence context.
 void setProperty(java.lang.String propertyName, java.lang.Object value)
          Set an entity manager property.
<T> T
unwrap(java.lang.Class<T> cls)
          Return an object of the specified type to allow access to the provider-specific API.
 

Method Detail

persist

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

Parameters:
entity -
Throws:
EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.)
java.lang.IllegalArgumentException - if the instance is not an entity
TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.

merge

<T> T merge(T entity)
Merge the state of the given entity into the current persistence context.

Parameters:
entity -
Returns:
the managed instance that the state was merged to
Throws:
java.lang.IllegalArgumentException - if instance is not an entity or is a removed entity
TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.

remove

void remove(java.lang.Object entity)
Remove the entity instance.

Parameters:
entity -
Throws:
java.lang.IllegalArgumentException - if the instance is not an entity or is a detached entity
TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.

find

<T> T find(java.lang.Class<T> entityClass,
           java.lang.Object primaryKey)
Find by primary key. Search for an entity of the specified class and primary key. If the entity instance is contained in the persistence context it is returned from there.

Parameters:
entityClass -
primaryKey -
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 is not a valid type for that entity's primary key or is null

find

<T> T find(java.lang.Class<T> entityClass,
           java.lang.Object primaryKey,
           java.util.Map<java.lang.String,java.lang.Object> properties)
Find by primary key, using the specified properties. Search for an entity of the specified class and primary key. If the entity instance is contained in the persistence context it is returned from there. If a vendor-specific property or hint is not recognized, it is silently ignored.

Parameters:
entityClass -
primaryKey -
properties - standard and vendor-specific properties
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 is not a valid type for that entity's primary key or is null

find

<T> T find(java.lang.Class<T> entityClass,
           java.lang.Object primaryKey,
           LockModeType lockMode)
Find by primary key and lock. Search for an entity of the specified class and primary key and lock it with respect to the specified lock type. If the entity instance is contained in the persistence context it is returned from there, and the effect of this method is the same as if the lock method had been called on the entity. If the entity is found within the persistence context and the lock mode type is pessimistic and the entity has a version attribute, the persistence provider must perform optimistic version checks when obtaining the database lock. If these checks fail, the OptimisticLockException will be thrown. If the lock mode type is pessimistic and the entity instance is found but cannot be locked: - the PessimisticLockException will be thrown if the database locking failure causes transaction-level rollback. - the LockTimeoutException will be thrown if the database locking failure causes only statement-level rollback

Parameters:
entityClass -
primaryKey -
lockMode -
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 or is null
TransactionRequiredException - if there is no transaction and a lock mode other than NONE is set
OptimisticLockException - if the optimistic version check fails
PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
PersistenceException - if an unsupported lock call is made

find

<T> T find(java.lang.Class<T> entityClass,
           java.lang.Object primaryKey,
           LockModeType lockMode,
           java.util.Map<java.lang.String,java.lang.Object> properties)
Find by primary key and lock, using the specified properties. Search for an entity of the specified class and primary key and lock it with respect to the specified lock type. If the entity instance is contained in the persistence context it is returned from there. If the entity is found within the persistence context and the lock mode type is pessimistic and the entity has a version attribute, the persistence provider must perform optimistic version checks when obtaining the database lock. If these checks fail, the OptimisticLockException will be thrown. If the lock mode type is pessimistic and the entity instance is found but cannot be locked: - the PessimisticLockException will be thrown if the database locking failure causes transaction-level rollback. - the LockTimeoutException will be thrown if the database locking failure causes only statement-level rollback If a vendor-specific property or hint is not recognized, it is silently ignored. Portable applications should not rely on the standard timeout hint. Depending on the database in use and the locking mechanisms used by the provider, the hint may or may not be observed.

Parameters:
entityClass -
primaryKey -
lockMode -
properties - standard and vendor-specific properties and hints
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 or is null
TransactionRequiredException - if there is no transaction and a lock mode other than NONE is set
OptimisticLockException - if the optimistic version check fails
PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
PersistenceException - if an unsupported lock call is made

getReference

<T> T getReference(java.lang.Class<T> entityClass,
                   java.lang.Object primaryKey)
Get an instance, whose state may be lazily fetched. If the requested instance does not exist in the database, the EntityNotFoundException is thrown 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:
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 or is null
EntityNotFoundException - if the entity state cannot be accessed

flush

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

Throws:
TransactionRequiredException - if there is no transaction
PersistenceException - if the flush fails

setFlushMode

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

Parameters:
flushMode -

getFlushMode

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

Returns:
flushMode

lock

void lock(java.lang.Object entity,
          LockModeType lockMode)
Lock an entity instance that is contained in the persistence context with the specified lock mode type. If a pessimistic lock mode type is specified and the entity contains a version attribute, the persistence provider must also perform optimistic version checks when obtaining the database lock. If these checks fail, the OptimisticLockException will be thrown. If the lock mode type is pessimistic and the entity instance is found but cannot be locked: - the PessimisticLockException will be thrown if the database locking failure causes transaction-level rollback. - the LockTimeoutException will be thrown if the database locking failure causes only statement-level rollback

Parameters:
entity -
lockMode -
Throws:
java.lang.IllegalArgumentException - if the instance is not an entity or is a detached entity
TransactionRequiredException - if there is no transaction
EntityNotFoundException - if the entity does not exist in the database when pessimistic locking is performed
OptimisticLockException - if the optimistic version check fails
PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
PersistenceException - if an unsupported lock call is made

lock

void lock(java.lang.Object entity,
          LockModeType lockMode,
          java.util.Map<java.lang.String,java.lang.Object> properties)
Lock an entity instance that is contained in the persistence context with the specified lock mode type and with specified properties. If a pessimistic lock mode type is specified and the entity contains a version attribute, the persistence provider must also perform optimistic version checks when obtaining the database lock. If these checks fail, the OptimisticLockException will be thrown. If the lock mode type is pessimistic and the entity instance is found but cannot be locked: - the PessimisticLockException will be thrown if the database locking failure causes transaction-level rollback. - the LockTimeoutException will be thrown if the database locking failure causes only statement-level rollback If a vendor-specific property or hint is not recognized, it is silently ignored. Portable applications should not rely on the standard timeout hint. Depending on the database in use and the locking mechanisms used by the provider, the hint may or may not be observed.

Parameters:
entity -
lockMode -
properties - standard and vendor-specific properties and hints
Throws:
java.lang.IllegalArgumentException - if the instance is not an entity or is a detached entity
TransactionRequiredException - if there is no transaction
EntityNotFoundException - if the entity does not exist in the database when pessimistic locking is performed
OptimisticLockException - if the optimistic version check fails
PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
PersistenceException - if an unsupported lock call is made

refresh

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

Parameters:
entity -
Throws:
java.lang.IllegalArgumentException - if the instance is not an entity or the entity is not managed
TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.
EntityNotFoundException - if the entity no longer exists in the database

refresh

void refresh(java.lang.Object entity,
             java.util.Map<java.lang.String,java.lang.Object> properties)
Refresh the state of the instance from the database, using the specified properties, and overwriting changes made to the entity, if any. If a vendor-specific property or hint is not recognized, it is silently ignored.

Parameters:
entity -
properties - standard and vendor-specific properties
Throws:
java.lang.IllegalArgumentException - if the instance is not an entity or the entity is not managed
TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.
EntityNotFoundException - if the entity no longer exists in the database

refresh

void refresh(java.lang.Object entity,
             LockModeType lockMode)
Refresh the state of the instance from the database, overwriting changes made to the entity, if any, and lock it with respect to given lock mode type. If the lock mode type is pessimistic and the entity instance is found but cannot be locked: - the PessimisticLockException will be thrown if the database locking failure causes transaction-level rollback. - the LockTimeoutException will be thrown if the database locking failure causes only statement-level rollback.

Parameters:
entity -
lockMode -
Throws:
java.lang.IllegalArgumentException - if the instance is not an entity or the entity is not managed
TransactionRequiredException - if there is no transaction
EntityNotFoundException - if the entity no longer exists in the database
PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
PersistenceException - if an unsupported lock call is made

refresh

void refresh(java.lang.Object entity,
             LockModeType lockMode,
             java.util.Map<java.lang.String,java.lang.Object> properties)
Refresh the state of the instance from the database, overwriting changes made to the entity, if any, and lock it with respect to given lock mode type and with specified properties. If the lock mode type is pessimistic and the entity instance is found but cannot be locked: - the PessimisticLockException will be thrown if the database locking failure causes transaction-level rollback. - the LockTimeoutException will be thrown if the database locking failure causes only statement-level rollback If a vendor-specific property or hint is not recognized, it is silently ignored. Portable applications should not rely on the standard timeout hint. Depending on the database in use and the locking mechanisms used by the provider, the hint may or may not be observed.

Parameters:
entity -
lockMode -
properties - standard and vendor-specific properties and hints
Throws:
java.lang.IllegalArgumentException - if the instance is not an entity or the entity is not managed
TransactionRequiredException - if there is no transaction
EntityNotFoundException - if the entity no longer exists in the database
PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
PersistenceException - if an unsupported lock call is made

clear

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.


detach

void detach(java.lang.Object entity)
Remove the given entity from the persistence context, causing a managed entity to become detached. Unflushed changes made to the entity if any (including removal of the entity), will not be synchronized to the database. Entities which previously referenced the detached entity will continue to reference it.

Parameters:
entity -
Throws:
java.lang.IllegalArgumentException - if the instance is not an entity

contains

boolean contains(java.lang.Object entity)
Check if the instance is a managed entity instance belonging to the current persistence context.

Parameters:
entity -
Returns:
Throws:
java.lang.IllegalArgumentException - if not an entity

getLockMode

LockModeType getLockMode(java.lang.Object entity)
Get the current lock mode for the entity instance.

Parameters:
entity -
Returns:
lock mode
Throws:
TransactionRequiredException - if there is no transaction
java.lang.IllegalArgumentException - if the instance is not a managed entity and a transaction is active

setProperty

void setProperty(java.lang.String propertyName,
                 java.lang.Object value)
Set an entity manager property. If a vendor-specific property is not recognized, it is silently ignored.

Parameters:
propertyName -
value -
Throws:
java.lang.IllegalArgumentException - if the second argument is not valid for the implementation

getProperties

java.util.Map<java.lang.String,java.lang.Object> getProperties()
Get the properties and associated values that are in effect for the entity manager. Changing the contents of the map does not change the configuration in effect.


getSupportedProperties

java.util.Set<java.lang.String> getSupportedProperties()
Get the names of the properties that are supported for use with the entity manager. These correspond to properties and hints that may be passed to the methods of the EntityManager interface that take a properties argument or used with the PersistenceContext annotation. These properties include all standard entity manager hints and properties as well as vendor-specific ones supported by the provider. These properties may or may not currently be in effect.

Returns:
property names

createQuery

Query createQuery(java.lang.String qlString)
Create an instance of Query for executing a Java Persistence query language statement.

Parameters:
qlString - a Java Persistence query string
Returns:
the new query instance
Throws:
java.lang.IllegalArgumentException - if the query string is found to be invalid

createQuery

<T> TypedQuery<T> createQuery(java.lang.String qlString,
                              java.lang.Class<T> resultClass)
Create an instance of TypedQuery for executing a Java Persistence query language statement.

Parameters:
qlString - a Java Persistence query string
resultClass - the type of the query result
Returns:
the new query instance
Throws:
java.lang.IllegalArgumentException - if the query string is found to be invalid

createQuery

<T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery)
Create an instance of TypedQuery for executing a criteria query.

Parameters:
criteriaQuery - a criteria query object
Returns:
the new query instance
Throws:
java.lang.IllegalArgumentException - if the query definition is found to be invalid

createNamedQuery

Query createNamedQuery(java.lang.String name)
Create an instance of Query for executing a named query (in the Java Persistence query language or in native SQL).

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 or if the query string is found to be invalid

createNamedQuery

<T> TypedQuery<T> createNamedQuery(java.lang.String name,
                                   java.lang.Class<T> resultClass)
Create an instance of TypedQuery for executing a named query (in the Java Persistence query language or in native SQL).

Parameters:
name - the name of a query defined in metadata
resultClass - the type of the query result
Returns:
the new query instance
Throws:
java.lang.IllegalArgumentException - if a query has not been defined with the given name or if the query string is found to be invalid

createNativeQuery

Query createNativeQuery(java.lang.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

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

Parameters:
sqlString - a native SQL query string
resultClass - the class of the resulting instance(s)
Returns:
the new query instance

createNativeQuery

Query createNativeQuery(java.lang.String sqlString,
                        java.lang.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

void joinTransaction()
Indicate 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.

Throws:
TransactionRequiredException - if there is no transaction.

unwrap

<T> T unwrap(java.lang.Class<T> cls)
Return an object of the specified type to allow access to the provider-specific API. If the provider's EntityManager implementation does not support the specified class, the PersistenceException is thrown.

Parameters:
cls - the class of the object to be returned. This is normally either the underlying EntityManager implementation class or an interface that it implements.
Returns:
an instance of the specified class
Throws:
PersistenceException - if the provider does not support the call.

getDelegate

java.lang.Object getDelegate()
Return the underlying provider object for the EntityManager, if available. The result of this method is implementation specific. The unwrap method is to be preferred for new applications.


close

void close()
Close an application-managed EntityManager. After the close method has been invoked, all methods on the EntityManager instance and any Query objects obtained from it will throw the IllegalStateException except for getProperties, getSupportedProperties, getTransaction, and isOpen (which will return false). If this method is called when the EntityManager is associated with an active transaction, the persistence context remains managed until the transaction completes.

Throws:
java.lang.IllegalStateException - if the EntityManager is container-managed.

isOpen

boolean isOpen()
Determine whether the EntityManager is open.

Returns:
true until the EntityManager has been closed.

getTransaction

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

Returns:
EntityTransaction instance
Throws:
java.lang.IllegalStateException - if invoked on a JTA EntityManager.

getEntityManagerFactory

EntityManagerFactory getEntityManagerFactory()
Return the entity manager factory for the entity manager.

Returns:
EntityManagerFactory instance
Throws:
java.lang.IllegalStateException - if the entity manager has been closed.

getQueryBuilder

QueryBuilder getQueryBuilder()
Return an instance of QueryBuilder for the creation of CriteriaQuery objects.

Returns:
QueryBuilder instance
Throws:
java.lang.IllegalStateException - if the entity manager has been closed.

getMetamodel

Metamodel getMetamodel()
Return an instance of Metamodel interface for access to the metamodel of the persistence unit.

Returns:
Metamodel instance
Throws:
java.lang.IllegalStateException - if the entity manager has been closed.

EclipseLink 2.0.0_ 2.0.0.v20090626-r4569 API Reference