EntityManager
- Super Interfaces:
AutoCloseable
An instance of EntityManager must be obtained from an EntityManagerFactory, and is only able to manage persistence of entities belonging to the associated persistence unit.
An application-managed EntityManager may be created via a call to createEntityManager(). The EntityManager must be explicitly closed via a call to close(), to allow resources to be cleaned up by the persistence provider. This approach places almost complete responsibility for cleanup and exception management on the client, and is thus considered quite error-prone. It is much safer to use the methods runInTransaction() and callInTransaction().
entityManagerFactory.runInTransaction(entityManager -> { // do work in a persistence context ... });
In the Jakarta EE environment, a container-managed EntityManager may be obtained by dependency injection, using PersistenceContext.
// inject the container-managed entity manager @PersistenceContext(unitName="orderMgt") EntityManager entityManager;
If the persistence unit has plain transaction management, transactions must be managed using the EntityTransaction obtained by calling getTransaction().
A complete idiom for custom application management of the EntityManager and its associated resource-local EntityTransaction is as follows:
EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin(); // do work ... transaction.commit(); } catch (Exception e) { if (transaction.isActive()) transaction.rollback(); throw e; } finally { entityManager.close(); }
Each EntityManager instance is associated with a distinct persistence context. A persistence context is a set of entity instances in which for any given persistent entity identity (defined by an entity type and primary key) there is at most one entity instance. The entity instances associated with a persistence context are considered managed objects, with a well-defined lifecycle under the control of the persistence provider.
Any entity instance can be characterized as being in one of the following lifecycle states:
- A new entity has no persistent identity, and is not yet associated with any persistence context.
- A managed entity is an instance with a persistent identity that is currently associated with a persistence context.
- A detached entity is an instance with a persistent identity that is not (or no longer) associated with any active persistence context.
- A removed entity is an instance with a persistent identity, and associated with a persistence context, that is scheduled for removal from the database upon transaction commit.
The EntityManager API is used to perform operations that affect the state of the persistence context, or that modify the lifecycle state of individual entity instances. The client may plain and plain instances, plain entities by their primary key, and execute plain which range over entity types. An entity may be disassociated from the persistence context by calling detach(), and a persistence context may be completely cleared, detaching all its entities, by calling clear().
The client may also make changes to the state of an entity instance by mutating the entity directly, or it may request that the state of a detached instance be plain, replacing the state of a managed instance with the same persistent identity. Note that there is no explicit "update" operation; since an entity is a managed object, modifications to its persistent fields and properties are automatically detected, as long as it is associated with an active persistence context.
Modifications to the state of entities associated with a persistence context are not immediately synchronized with the database. Synchronization happens during a process called flush. The timing of the flush process depends on the plain, which may be set explicitly by calling setFlushMode().
- For FlushModeType.COMMIT, the persistence context is flushed before the transaction commits.
- For FlushModeType.AUTO, which is the default, the persistence context must also be flushed before execution of any query whose result set would be affected by unflushed modifications to entities associated with the persistence context.
At any given moment, a persistence context might hold an optimistic or pessimistic lock on an entity instance. The full range of possible lock types is enumerated by LockModeType. Some operations of this interface, including the methods lock(), refresh(), and find(), accept an explicit LockModeType, allowing the client to request a specific type of lock.
Interaction of the persistence context (or first-level cache) with the second-level cache, if any, may be controlled by calling setCacheRetrieveMode() and setCacheStoreMode().
Some operations accept one or more built-in and vendor-specific options:
- find() and find() accept FindOptions,
- refresh() accepts RefreshOptions, and
- lock() accepts LockOptions.
- Since:
- JPA 1.0
EntityManager.Public Methods
java.sql.Connection. If this EntityManager is associated with a transaction, the function is executed in the context of the transaction. The given function should close any resources it creates, but should not close the connection itself, nor commit or roll back the transaction. If the given action throws an exception, the persistence provider must mark the transaction for rollback. - Parameters:
function- the function< C >- the connection type, usually {@code java.sql.Connection}< T >- the type of result returned by the function
- Return:
- the value returned by {@link ConnectionFunction#apply} .
- Throws:
- PersistenceException - wrapping the checked {@link Exception} thrown by {@link ConnectionFunction#apply} , if any
- Since:
- JPA 3.2
- Since:
- JPA 1.0
After invocation of close(), every method of the EntityManager instance and of any instance of Query, TypedQuery, or StoredProcedureQuery obtained from it throws the IllegalStateException, except for getProperties(), getTransaction(), and isOpen() (which returns 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 value indicating if entity belongs to the 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
- See Also:
- NamedQuery
- NamedNativeQuery
- 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 is 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 no query has been defined with the given name
- Since:
- JPA 2.1
If the query is not an update or delete query, query execution will result in each row of the SQL result being returned as a result of type Object[] (or a result of type Object if there is only one column in the select list.) Column values are returned in the order of their occurrence in the select list and default JDBC type mappings are applied.
- Parameters:
sqlString- a native SQL query string
- Return:
- the new query instance
- Since:
- JPA 1.0
In the next release of this API, the return type of this method will change to TypedQuery<T>.
- Parameters:
sqlString- a native SQL query stringresultClass- the type of the query result
- 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:
selectQuery- a criteria query object
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if the criteria query is found to be invalid
- Since:
- JPA 3.2
- 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 Jakarta 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 Jakarta 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:
reference- a reference to the query defined in metadata
- Return:
- the new query instance
- Throws:
- IllegalArgumentException - if a query has not been defined, 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 1.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 is 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 if 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 is 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 if 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 is 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- a managed or removed 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 if the second argument 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 is thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the
PessimisticLockExceptionis thrown if the database locking failure causes transaction-level rollback - the
LockTimeoutExceptionis thrown if the database locking failure causes only statement-level rollback
If a vendor-specific plain is not recognized, it is silently ignored.
Portable applications should not rely on the standard plain. Depending on the database in use and the locking mechanisms used by the provider, this option may or may not be observed.
- Parameters:
entityClass- entity classprimaryKey- primary keyoptions- standard and vendor-specific options
- Return:
- the found entity instance or null if the entity does not exist
- Throws:
- IllegalArgumentException - if there are contradictory options, if the first argument does not denote an entity type belonging to the persistence unit, or if the second argument is not a valid non-null instance of the entity primary key type
- TransactionRequiredException - if there is no transaction and a lock mode other than {@code NONE} is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other than {@code NONE} 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 {@code NONE} is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other than {@code NONE} is specified
- Since:
- JPA 3.2
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 is thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException is 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 {@code NONE} is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other than {@code NONE} 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 {@code NONE} is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other than {@code NONE} is specified
- 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 is thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException is 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 {@code NONE} is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other than {@code NONE} 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 {@code NONE} is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other than {@code NONE} is specified
- 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 if the second argument is not a valid type for that entity's primary key or is null
- 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 is thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException is thrown if the database locking failure causes only statement-level rollback
If a vendor-specific plain is not recognized, it is silently ignored.
Portable applications should not rely on the standard plain. Depending on the database in use and the locking mechanisms used by the provider, this option may or may not be observed.
- Parameters:
entityGraph- entity graph interpreted as a load graphprimaryKey- primary keyoptions- standard and vendor-specific options
- Return:
- the found entity instance or null if the entity does not exist
- Throws:
- IllegalArgumentException - if there are contradictory options, if the first argument does not denote an entity type belonging to the persistence unit, or if the second argument is not a valid non-null instance of the entity primary key type
- TransactionRequiredException - if there is no transaction and a lock mode other than {@code NONE} is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other than {@code NONE} 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 {@code NONE} is specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other than {@code NONE} is specified
- Since:
- JPA 3.2
- 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
- Since:
- JPA 3.2
- Since:
- JPA 3.2
- Return:
- an instance of {@link CriteriaBuilder}
- Throws:
- IllegalStateException - if the entity manager has been closed
- See Also:
- getCriteriaBuilder()
- Since:
- JPA 2.0
The unwrap method is to be preferred for new applications.
- Return:
- the underlying provider object
- Since:
- JPA 1.0
EntityGraph should be considered immutable. - Parameters:
graphName- name of an existing entity graph
- Return:
- named entity graph
- Throws:
- IllegalArgumentException - if there is no entity of graph with 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:
- the {@link EntityManagerFactory}
- Throws:
- IllegalStateException - if the entity manager has been closed
- Since:
- JPA 2.0
- Return:
- the current {@link FlushModeType}
- Since:
- JPA 1.0
- Parameters:
entity- a managed entity instance
- Return:
- the lock mode currently held
- Throws:
- TransactionRequiredException - if there is no active transaction or if the entity manager has not been joined to the current transaction
- IllegalArgumentException - if a transaction is active but the given instance is not a managed entity
- Since:
- JPA 2.0
- Return:
- an instance of {@link Metamodel}
- Throws:
- IllegalStateException - if the entity manager has been closed
- Since:
- JPA 2.0
- Return:
- a map of properties and hints currently in effect
- Since:
- JPA 2.0
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 but not required to throw the EntityNotFoundException when getReference() is called.)
This operation allows the application to create an association to an entity without loading its state from the database.
The application should not expect the instance state to be available upon detachment, unless it was accessed by the application while the entity manager was open.
- Parameters:
entityClass- entity classprimaryKey- primary key
- Return:
- a reference to the 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
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 but not required to throw the EntityNotFoundException when getReference() is called.)
This operation allows the application to create an association to an entity without loading its state from the database.
The application should not expect the instance state to be available upon detachment, unless it was accessed by the application while the entity manager was open.
- Parameters:
entity- a persistent or detached entity instance
- Return:
- a reference to the entity instance
- Throws:
- IllegalArgumentException - if the given object is not an entity, or if it is neither persistent nor detached
- EntityNotFoundException - if the entity state cannot be accessed
- Since:
- JPA 3.2
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 active 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 is thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException is thrown if the database locking failure causes only statement-level rollback
- Parameters:
entity- a managed 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 is thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException is thrown if the database locking failure causes only statement-level rollback
If a vendor-specific LockOption is not recognized, it is silently ignored.
Portable applications should not rely on the standard plain. Depending on the database in use and the locking mechanisms used by the provider, the option may or may not be observed.
- Parameters:
entity- a managed entity instancelockMode- lock modeoptions- standard and vendor-specific options
- 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 3.2
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 is thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException ia 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- a managed 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- a new, managed, or detached entity instance
- Return:
- the managed instance that the state was merged to
- Throws:
- IllegalArgumentException - if the 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 {@link PersistenceContextType#TRANSACTION}
- Since:
- JPA 1.0
- Parameters:
entity- a new, managed, or removed entity instance
- Throws:
- EntityExistsException - if the given entity is detached (if the entity is detached, the {@code EntityExistsException} may be thrown when the persist operation is invoked, or the {@code EntityExistsException} or another {@code PersistenceException} may be thrown at flush or commit time)
- IllegalArgumentException - if the given instance is not an entity
- TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager that is of type {@link PersistenceContextType#TRANSACTION}
- IllegalArgumentException - if the given instance is not an entity
- Since:
- JPA 1.0
- Parameters:
entity- a managed 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 {@link 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 {@link PersistenceContextType#TRANSACTION}
- Since:
- JPA 1.0
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException is thrown if the database locking failure causes only statement-level rollback.
- Parameters:
entity- a managed entity instancelockMode- lock mode
- Throws:
- IllegalArgumentException - if the instance is not an entity or if the entity is not managed
- TransactionRequiredException - if invoked on a container-managed entity manager of type {@link PersistenceContextType#TRANSACTION} when there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other than {@link LockModeType#NONE} was specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other than {@code NONE} was 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 {@link PersistenceContextType#TRANSACTION} when there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other than {@link LockModeType#NONE} was specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other than {@code NONE} was specified
- Since:
- JPA 2.0
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException is 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- a managed entity instancelockMode- lock modeproperties- standard and vendor-specific properties and hints
- Throws:
- IllegalArgumentException - if the instance is not an entity or if the entity is not managed
- TransactionRequiredException - if invoked on a container-managed entity manager of type {@link PersistenceContextType#TRANSACTION} when there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other than {@link LockModeType#NONE} was specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other than {@code NONE} was 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 {@link PersistenceContextType#TRANSACTION} when there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other than {@link LockModeType#NONE} was specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other than {@code NONE} was specified
- Since:
- JPA 2.0
If a vendor-specific property or hint is not recognized, it is silently ignored.
- Parameters:
entity- a managed 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 {@link 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 {@link PersistenceContextType#TRANSACTION}
- Since:
- JPA 2.0
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
- the PessimisticLockException is thrown if the database locking failure causes transaction-level rollback
- the LockTimeoutException is thrown if the database locking failure causes only statement-level rollback.
If a vendor-specific RefreshOption is not recognized, it is silently ignored.
Portable applications should not rely on the standard plain. Depending on the database in use and the locking mechanisms used by the provider, the hint may or may not be observed.
- Parameters:
entity- a managed entity instanceoptions- standard and vendor-specific options
- Throws:
- IllegalArgumentException - if the instance is not an entity or if the entity is not managed
- TransactionRequiredException - if invoked on a container-managed entity manager of type {@link PersistenceContextType#TRANSACTION} when there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other than {@link LockModeType#NONE} was specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other than {@code NONE} was 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 {@link PersistenceContextType#TRANSACTION} when there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other than {@link LockModeType#NONE} was specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other than {@code NONE} was specified
- Since:
- JPA 3.2
- Parameters:
entity- a managed, new, or removed 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 {@link PersistenceContextType#TRANSACTION} and there is no transaction
- Since:
- JPA 1.0
java.sql.Connection. If this EntityManager is associated with a transaction, the action is executed in the context of the transaction. The given action should close any resources it creates, but should not close the connection itself, nor commit or roll back the transaction. If the given action throws an exception, the persistence provider must mark the transaction for rollback. - Parameters:
action- the action< C >- the connection type, usually {@code java.sql.Connection}
- Throws:
- PersistenceException - wrapping the checked {@link Exception} thrown by {@link ConnectionConsumer#accept} , if any
- Since:
- JPA 3.2
- Parameters:
cacheRetrieveMode- cache retrieval mode
- Since:
- JPA 3.2
- Parameters:
cacheStoreMode- cache storage mode
- Since:
- JPA 3.2
- Parameters:
flushMode- flush mode
- Since:
- JPA 1.0
- Parameters:
propertyName- name of the property or hintvalue- value for the property or hint
- Throws:
- IllegalArgumentException - if the property or hint name is recognized by the implementation, but the second argument is not valid value
- Since:
- JPA 2.0
EntityManager does not support the given type, the PersistenceException is thrown. - Parameters:
cls- the class of the object to be returned. This is usually either the underlying class implementing {@code EntityManager} or an interface it implements.
- Return:
- an instance of the specified class
- Throws:
- PersistenceException - if the provider does not support the given type
- Since:
- JPA 2.0