EntityManager
An EntityManager
instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed. The EntityManager
API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.
The set of entities that can be managed by a given EntityManager
instance is defined by a persistence unit. A persistence unit defines the set of all classes that are related or grouped by the application, and which must be colocated in their mapping to a single database.
- Since:
- JPA 1.0
EntityManager
.Public Methods
- Since:
- JPA 1.0
EntityManager
instance and any Query
, TypedQuery
, and StoredProcedureQuery
objects obtained from it will throw the IllegalStateException
except for getProperties
, getTransaction
, and isOpen
(which will return false). If this method is called when the entity manager is joined to an active transaction, the persistence context remains managed until the transaction completes. - Throws:
- IllegalStateException - if the entity manager is container-managed
- Since:
- JPA 1.0
- Parameters:
entity
- entity instance
- Return:
- boolean indicating if entity is in persistence context
- Throws:
- IllegalArgumentException - if not an entity
- Since:
- JPA 1.0
- Parameters:
rootType
- class of entity graph
- Return:
- entity graph
- Since:
- JPA 2.1
- Parameters:
graphName
- name of an entity graph
- Return:
- entity graph
- Since:
- JPA 2.1
- Parameters:
name
- the name of a query defined in metadata
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if a query has not been defined with the given name or if the query string is found to be invalid
- Since:
- JPA 1.0
resultClass
argument. - Parameters:
name
- the name of a query defined in metadataresultClass
- the type of the query result
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if a query has not been defined with the given name or if the query string is found to be invalid or if the query result is found to not be assignable to the specified type
- Since:
- JPA 2.0
Parameters must be registered before the stored procedure can be executed.
If the stored procedure returns one or more result sets, any result set will be returned as a list of type Object[].
- Parameters:
name
- name assigned to the stored procedure query in metadata
- Return:
- the new stored procedure query instance
- Throws:
- IllegalArgumentException - if a query has not been defined with the given name
- Since:
- JPA 2.1
- Parameters:
sqlString
- a native SQL query string
- Return:
- the new query instance
- Since:
- JPA 1.0
- Parameters:
sqlString
- a native SQL query stringresultClass
- the class of the resulting instance(s)
- Return:
- the new query instance
- Since:
- JPA 1.0
- Parameters:
sqlString
- a native SQL query stringresultSetMapping
- the name of the result set mapping
- Return:
- the new query instance
- Since:
- JPA 1.0
- Parameters:
deleteQuery
- a criteria delete query object
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if the delete query is found to be invalid
- Since:
- JPA 2.1
- Parameters:
criteriaQuery
- a criteria query object
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if the criteria query is found to be invalid
- Since:
- JPA 2.0
- Parameters:
updateQuery
- a criteria update query object
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if the update query is found to be invalid
- Since:
- JPA 2.1
- Parameters:
qlString
- a Java Persistence query string
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if the query string is found to be invalid
- Since:
- JPA 1.0
resultClass
argument. - Parameters:
qlString
- a Java Persistence query stringresultClass
- the type of the query result
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if the query string is found to be invalid or if the query result is found to not be assignable to the specified type
- Since:
- JPA 2.0
Parameters must be registered before the stored procedure can be executed.
If the stored procedure returns one or more result sets, any result set will be returned as a list of type Object[].
- Parameters:
procedureName
- name of the stored procedure in the database
- Return:
- the new stored procedure query instance
- Throws:
- IllegalArgumentException - if a stored procedure of the given name does not exist (or the query execution will fail)
- Since:
- JPA 2.1
Parameters must be registered before the stored procedure can be executed.
The resultClass
arguments must be specified in the order in which the result sets will be returned by the stored procedure invocation.
- Parameters:
procedureName
- name of the stored procedure in the databaseresultClasses
- classes to which the result sets produced by the stored procedure are to be mapped
- Return:
- the new stored procedure query instance
- Throws:
- IllegalArgumentException - if a stored procedure of the given name does not exist (or the query execution will fail)
- Since:
- JPA 2.1
Parameters must be registered before the stored procedure can be executed.
The resultSetMapping
arguments must be specified in the order in which the result sets will be returned by the stored procedure invocation.
- Parameters:
procedureName
- name of the stored procedure in the databaseresultSetMappings
- the names of the result set mappings to be used in mapping result sets returned by the stored procedure
- Return:
- the new stored procedure query instance
- Throws:
- IllegalArgumentException - if a stored procedure or result set mapping of the given name does not exist (or the query execution will fail)
- Since:
- JPA 1.0
- Parameters:
entity
- entity instance
- Throws:
- IllegalArgumentException - if the instance is not an entity
- Since:
- JPA 2.0
- Parameters:
entityClass
- entity classprimaryKey
- primary key
- Return:
- 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 is not a valid type for that entity's primary key or is null
- Since:
- JPA 1.0
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
- entity classprimaryKey
- primary keylockMode
- lock mode
- Return:
- 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 entity's primary key or is null
- TransactionRequiredException - if there is no transaction and a lock mode other than
NONE
is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other thanNONE
is specified- 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
- TransactionRequiredException - if there is no transaction and a lock mode other than
- Since:
- JPA 2.0
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
- entity classprimaryKey
- primary keylockMode
- lock modeproperties
- standard and vendor-specific properties and hints
- Return:
- 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 entity's primary key or is null
- TransactionRequiredException - if there is no transaction and a lock mode other than
NONE
is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other thanNONE
is specified- 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
- TransactionRequiredException - if there is no transaction and a lock mode other than
- Since:
- JPA 2.0
- Parameters:
entityClass
- entity classprimaryKey
- primary keyproperties
- standard and vendor-specific properties and hints
- Return:
- 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 is not a valid type for that entity's primary key or is null
- Since:
- JPA 2.0
- Throws:
- TransactionRequiredException - if there is no transaction or if the entity manager has not been joined to the current transaction
- PersistenceException - if the flush fails
- Since:
- JPA 1.0
- Return:
- CriteriaBuilder instance
- Throws:
- IllegalStateException - if the entity manager has been closed
- Since:
- JPA 2.0
The unwrap
method is to be preferred for new applications.
- Return:
- underlying provider object for EntityManager
- Since:
- JPA 1.0
- Parameters:
graphName
- name of an existing entity graph
- Return:
- named entity graph
- Throws:
- IllegalArgumentException - if there is no EntityGraph of the given name
- Since:
- JPA 2.1
- Parameters:
entityClass
- entity class
- Return:
- list of all entity graphs defined for the entity
- Throws:
- IllegalArgumentException - if the class is not an entity
- Since:
- JPA 2.1
- Return:
- EntityManagerFactory instance
- Throws:
- IllegalStateException - if the entity manager has been closed
- Since:
- JPA 2.0
- Return:
- flushMode
- Since:
- JPA 1.0
- Parameters:
entity
- entity instance
- Return:
- lock mode
- Throws:
- TransactionRequiredException - if there is no transaction or if the entity manager has not been joined to the current transaction
- IllegalArgumentException - if the instance is not a managed entity and a transaction is active
- Since:
- JPA 2.0
- Return:
- Metamodel instance
- Throws:
- IllegalStateException - if the entity manager has been closed
- Since:
- JPA 2.0
- Return:
- map of properties and hints in effect for entity manager
- Since:
- JPA 2.0
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
- entity classprimaryKey
- primary key
- Return:
- 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 or is null
- EntityNotFoundException - if the entity state cannot be accessed
- Since:
- JPA 1.0
EntityTransaction
instance may be used serially to begin and commit multiple transactions. - Return:
- EntityTransaction instance
- Throws:
- IllegalStateException - if invoked on a JTA entity manager
- Since:
- JPA 1.0
- Return:
- boolean
- Since:
- JPA 2.1
- Return:
- true until the entity manager has been closed
- Since:
- JPA 1.0
This method should be called on a JTA application managed entity manager that was created outside the scope of the active transaction or on an entity manager of type SynchronizationType.UNSYNCHRONIZED
to associate it with the current JTA transaction.
- Throws:
- TransactionRequiredException - if there is no transaction
- Since:
- JPA 1.0
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
- entity instancelockMode
- lock mode
- Throws:
- IllegalArgumentException - if the instance is not an entity or is a detached entity
- TransactionRequiredException - if there is no transaction or if invoked on an entity manager which has not been joined to the current 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
- TransactionRequiredException - if there is no transaction or if invoked on an entity manager which has not been joined to the current transaction
- Since:
- JPA 1.0
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
- entity instancelockMode
- lock modeproperties
- standard and vendor-specific properties and hints
- Throws:
- IllegalArgumentException - if the instance is not an entity or is a detached entity
- TransactionRequiredException - if there is no transaction or if invoked on an entity manager which has not been joined to the current 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
- TransactionRequiredException - if there is no transaction or if invoked on an entity manager which has not been joined to the current transaction
- Since:
- JPA 2.0
- Parameters:
entity
- entity instance
- Return:
- the managed instance that the state was merged to
- Throws:
- IllegalArgumentException - if instance is not an entity or is a removed entity
- TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of that is of type
PersistenceContextType.TRANSACTION
- TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of that is of type
- Since:
- JPA 1.0
- Parameters:
entity
- entity instance
- Throws:
- EntityExistsException - if the entity already exists. (If the entity already exists, the
EntityExistsException
may be thrown when the persist operation is invoked, or theEntityExistsException
or anotherPersistenceException
may be thrown at flush or commit time.)- IllegalArgumentException - if the instance is not an entity
- TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of that is of type
PersistenceContextType.TRANSACTION
- IllegalArgumentException - if the instance is not an entity
- Since:
- JPA 1.0
- Parameters:
entity
- entity instance
- Throws:
- IllegalArgumentException - if the instance is not an entity or the entity is not managed
- TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of type
PersistenceContextType.TRANSACTION
- EntityNotFoundException - if the entity no longer exists in the database
- TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of type
- Since:
- JPA 1.0
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
- entity instancelockMode
- lock mode
- Throws:
- 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
when there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other thanNONE
has been specified; or if invoked on an extended entity manager that has not been joined to the current transaction and a lock mode other thanNONE
has been specified- 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
- TransactionRequiredException - if invoked on a container-managed entity manager of type
- Since:
- JPA 2.0
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
- entity instancelockMode
- lock modeproperties
- standard and vendor-specific properties and hints
- Throws:
- 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
when there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other thanNONE
has been specified; or if invoked on an extended entity manager that has not been joined to the current transaction and a lock mode other thanNONE
has been specified- 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
- TransactionRequiredException - if invoked on a container-managed entity manager of type
- Since:
- JPA 2.0
If a vendor-specific property or hint is not recognized, it is silently ignored.
- Parameters:
entity
- entity instanceproperties
- standard and vendor-specific properties and hints
- Throws:
- IllegalArgumentException - if the instance is not an entity or the entity is not managed
- TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of type
PersistenceContextType.TRANSACTION
- EntityNotFoundException - if the entity no longer exists in the database
- TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of type
- Since:
- JPA 2.0
- Parameters:
entity
- entity instance
- Throws:
- 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 - TransactionRequiredException - if invoked on a container-managed entity manager of type
- Since:
- JPA 1.0
- Parameters:
flushMode
- flush mode
- Since:
- JPA 1.0
- Parameters:
propertyName
- name of property or hintvalue
- value for property or hint
- Throws:
- IllegalArgumentException - if the second argument is not valid for the implementation
- Since:
- JPA 2.0
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 underlyingEntityManager
implementation class or an interface that it implements.
- Return:
- an instance of the specified class
- Throws:
- PersistenceException - if the provider does not support the call
- Since:
- JPA 2.0