jakarta.persistence.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 EntityManagerFactory.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 EntityManagerFactory.runInTransaction and EntityManagerFactory.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 resource local 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 persist and remove instances, find entities by their primary key, and execute queries 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 merged, 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 flush mode, 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.
flush. 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:
findandfindacceptFindOptions,refreshacceptsRefreshOptions, andlockacceptsLockOptions.
- See Also:
- Since:
- Jakarta Persistence (JPA) 1.0
EntityManager.Public Instance 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
- Returns:
- the value returned by
ConnectionFunction.apply..
- Throws:
- wrapping the checkedPersistenceExceptionExceptionthrown byConnectionFunction.apply, if any.
- Since:
- Jakarta Persistence (JPA) 3.2
void clear()- Since:
- Jakarta Persistence (JPA) 1.0
void close()After invocation of close(), every method of the EntityManager instance and of any instance of Query, TypedQuery<X>, 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.
AutoCloseable.java.lang.AutoCloseable/close()- Throws:
- if the entity manager is container-managed.IllegalStateException
- Since:
- Jakarta Persistence (JPA) 1.0
- Parameters:
entity- entity instance
- Returns:
- boolean value indicating if entity belongs to the persistence context.
- Throws:
- if not an entity.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
- Parameters:
rootType- class of entity graph
- Returns:
- entity graph.
- Since:
- Jakarta Persistence (JPA) 2.1
- Parameters:
graphName- name of an entity graph
- Returns:
- entity graph.
- Since:
- Jakarta Persistence (JPA) 2.1
- Parameters:
name- the name of a query defined in metadata
- Returns:
- the new query instance.
- Throws:
- if a query has not been defined with the given name or if the query string is found to be invalid.IllegalArgumentException
- See Also:
- Since:
- Jakarta Persistence (JPA) 1.0
resultClass argument.- Parameters:
name- the name of a query defined in metadataresultClass- the type of the query result
- Returns:
- the new query instance.
- Throws:
- 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.IllegalArgumentException
- Since:
- Jakarta Persistence (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
- Returns:
- the new stored procedure query instance.
- Throws:
- if no query has been defined with the given name.IllegalArgumentException
- Since:
- Jakarta Persistence (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
- Returns:
- the new query instance.
- Since:
- Jakarta Persistence (JPA) 1.0
In the next release of this API, the return type of this method will change to TypedQuery.
- Parameters:
sqlString- a native SQL query stringresultClass- the type of the query result
- Returns:
- the new query instance.
- Since:
- Jakarta Persistence (JPA) 1.0
- Parameters:
resultSetMapping- the name of the result set mappingsqlString- a native SQL query string
- Returns:
- the new query instance.
- Since:
- Jakarta Persistence (JPA) 1.0
- Parameters:
qlString- a Jakarta Persistence query string
- Returns:
- the new query instance.
- Throws:
- if the query string is found to be invalid.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
- Parameters:
criteriaQuery- a criteria query object
- Returns:
- the new query instance.
- Throws:
- if the criteria query is found to be invalid.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
- Parameters:
selectQuery- a criteria query object
- Returns:
- the new query instance.
- Throws:
- if the criteria query is found to be invalid.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 3.2
- Parameters:
updateQuery- a criteria update query object
- Returns:
- the new query instance.
- Throws:
- if the update query is found to be invalid.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.1
- Parameters:
deleteQuery- a criteria delete query object
- Returns:
- the new query instance.
- Throws:
- if the delete query is found to be invalid.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.1
resultClass argument.- Parameters:
qlString- a Jakarta Persistence query stringresultClass- the type of the query result
- Returns:
- the new query instance.
- Throws:
- if the query string is found to be invalid or if the query result is found to not be assignable to the specified type.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
- Parameters:
reference- a reference to the query defined in metadata
- Returns:
- the new query instance.
- Throws:
- 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.IllegalArgumentException
- See Also:
- Since:
- Jakarta Persistence (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
- Returns:
- the new stored procedure query instance.
- Throws:
- if a stored procedure of the given name does not exist (or if query execution will fail).IllegalArgumentException
- Since:
- Jakarta Persistence (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:
resultClasses- classes to which the result sets produced by the stored procedure are to be mappedprocedureName- name of the stored procedure in the database
- Returns:
- the new stored procedure query instance.
- Throws:
- if a stored procedure of the given name does not exist (or if query execution will fail).IllegalArgumentException
- Since:
- Jakarta Persistence (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:
resultSetMappings- the names of the result set mappings to be used in mapping result sets returned by the stored procedureprocedureName- name of the stored procedure in the database
- Returns:
- the new stored procedure query instance.
- Throws:
- if a stored procedure or result set mapping of the given name does not exist (or the query execution will fail).IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
cascade=DETACH. If the given entity instance is new or detached, that is, if it is not associated with this persistence context, it is ignored.- Parameters:
entity- a managed or removed entity instance
- Throws:
- if the instance is not an entity.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
- Parameters:
entityClass- entity classprimaryKey- primary key
- Returns:
- the found entity instance or null if the entity does not exist.
- Throws:
- 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.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
- Parameters:
entityClass- entity classproperties- standard and vendor-specific properties and hintsprimaryKey- primary key
- Returns:
- the found entity instance or null if the entity does not exist.
- Throws:
- 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.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
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 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
- Parameters:
entityClass- entity classlockMode- lock modeprimaryKey- primary key
- Returns:
- the found entity instance or null if the entity does not exist.
- Throws:
- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- 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.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if the optimistic version check fails.OptimisticLockException- if there is no transaction and a lock mode other thanTransactionRequiredExceptionNONEis specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other thanNONEis specified.
- Since:
- Jakarta Persistence (JPA) 2.0
T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String,Object> properties) 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 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 classlockMode- lock modeproperties- standard and vendor-specific properties and hintsprimaryKey- primary key
- Returns:
- the found entity instance or null if the entity does not exist.
- Throws:
- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- 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.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if the optimistic version check fails.OptimisticLockException- if there is no transaction and a lock mode other thanTransactionRequiredExceptionNONEis specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other thanNONEis specified.
- Since:
- Jakarta Persistence (JPA) 2.0
LockModeType, 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 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 option is not recognized, it is silently ignored.
Portable applications should not rely on the standard timeout option. Depending on the database in use and the locking mechanisms used by the provider, this option may or may not be observed.
- Parameters:
options- standard and vendor-specific optionsentityClass- entity classprimaryKey- primary key
- Returns:
- the found entity instance or null if the entity does not exist.
- Throws:
- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- 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.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if the optimistic version check fails.OptimisticLockException- if there is no transaction and a lock mode other thanTransactionRequiredExceptionNONEis specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other thanNONEis specified.
- Since:
- Jakarta Persistence (JPA) 3.2
LockModeType, 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 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 option is not recognized, it is silently ignored.
Portable applications should not rely on the standard timeout option. Depending on the database in use and the locking mechanisms used by the provider, this option may or may not be observed.
- Parameters:
options- standard and vendor-specific optionsentityGraph- entity graph interpreted as a load graphprimaryKey- primary key
- Returns:
- the found entity instance or null if the entity does not exist.
- Throws:
- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- 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.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if the optimistic version check fails.OptimisticLockException- if there is no transaction and a lock mode other thanTransactionRequiredExceptionNONEis specified or if invoked on an entity manager which has not been joined to the current transaction and a lock mode other thanNONEis specified.
- Since:
- Jakarta Persistence (JPA) 3.2
void flush()- Throws:
- if the flush fails.PersistenceException- if there is no transaction or if the entity manager has not been joined to the current transaction.TransactionRequiredException
- Since:
- Jakarta Persistence (JPA) 1.0
- Since:
- Jakarta Persistence (JPA) 3.2
- Since:
- Jakarta Persistence (JPA) 3.2
- Returns:
- an instance of
CriteriaBuilder.
- Throws:
- if the entity manager has been closed.IllegalStateException
- See Also:
- Since:
- Jakarta Persistence (JPA) 2.0
Object getDelegate()The unwrap method is to be preferred for new applications.
- Returns:
- the underlying provider object.
- Since:
- Jakarta Persistence (JPA) 1.0
EntityGraph should be considered immutable.- Parameters:
graphName- name of an existing entity graph
- Returns:
- named entity graph.
- Throws:
- if there is no entity of graph with the given name.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.1
- Parameters:
entityClass- entity class
- Returns:
- list of all entity graphs defined for the entity.
- Throws:
- if the class is not an entity.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.1
- Returns:
- the
EntityManagerFactory.
- Throws:
- if the entity manager has been closed.IllegalStateException
- Since:
- Jakarta Persistence (JPA) 2.0
- Returns:
- the current
FlushModeType.
- Since:
- Jakarta Persistence (JPA) 1.0
- Parameters:
entity- a managed entity instance
- Returns:
- the lock mode currently held.
- Throws:
- if a transaction is active but the given instance is not a managed entity.IllegalArgumentException- if there is no active transaction or if the entity manager has not been joined to the current transaction.TransactionRequiredException
- Since:
- Jakarta Persistence (JPA) 2.0
- Returns:
- an instance of
Metamodel.
- Throws:
- if the entity manager has been closed.IllegalStateException
- Since:
- Jakarta Persistence (JPA) 2.0
Map<String,Object> getProperties()- Returns:
- a map of properties and hints currently in effect.
- Since:
- Jakarta Persistence (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
- Returns:
- a reference to the entity instance.
- Throws:
- if the entity state cannot be accessed.EntityNotFoundException- 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.IllegalArgumentException
- Since:
- Jakarta Persistence (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
- Returns:
- a reference to the entity instance.
- Throws:
- if the entity state cannot be accessed.EntityNotFoundException- if the given object is not an entity, or if it is neither persistent nor detached.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 3.2
EntityTransaction instance may be used serially to begin and commit multiple transactions.- Returns:
- EntityTransaction instance.
- Throws:
- if invoked on a JTA entity manager.IllegalStateException
- Since:
- Jakarta Persistence (JPA) 1.0
boolean isJoinedToTransaction()- Returns:
- boolean.
- Since:
- Jakarta Persistence (JPA) 2.1
boolean isOpen()- Returns:
- true until the entity manager has been closed.
- Since:
- Jakarta Persistence (JPA) 1.0
void joinTransaction()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:
- if there is no active transaction.TransactionRequiredException
- Since:
- Jakarta Persistence (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
PessimisticLockExceptionis thrown if the database locking failure causes transaction-level rollback - the
LockTimeoutExceptionis thrown if the database locking failure causes only statement-level rollback
- Parameters:
lockMode- lock modeentity- a managed entity instance
- Throws:
- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if the entity does not exist in the database when pessimistic locking is performed.EntityNotFoundException- if the instance is not an entity or is a detached entity.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if the optimistic version check fails.OptimisticLockException- if there is no transaction or if invoked on an entity manager which has not been joined to the current transaction.TransactionRequiredException
- Since:
- Jakarta Persistence (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
PessimisticLockExceptionis thrown if the database locking failure causes transaction-level rollback - the
LockTimeoutExceptionia 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:
lockMode- lock modeentity- a managed entity instanceproperties- standard and vendor-specific properties and hints
- Throws:
- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if the entity does not exist in the database when pessimistic locking is performed.EntityNotFoundException- if the instance is not an entity or is a detached entity.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if the optimistic version check fails.OptimisticLockException- if there is no transaction or if invoked on an entity manager which has not been joined to the current transaction.TransactionRequiredException
- Since:
- Jakarta Persistence (JPA) 2.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
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 LockOption is not recognized, it is silently ignored.
Portable applications should not rely on the standard timeout option. Depending on the database in use and the locking mechanisms used by the provider, the option may or may not be observed.
- Parameters:
options- standard and vendor-specific optionslockMode- lock modeentity- a managed entity instance
- Throws:
- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if the entity does not exist in the database when pessimistic locking is performed.EntityNotFoundException- if the instance is not an entity or is a detached entity.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if the optimistic version check fails.OptimisticLockException- if there is no transaction or if invoked on an entity manager which has not been joined to the current transaction.TransactionRequiredException
- Since:
- Jakarta Persistence (JPA) 3.2
cascade=MERGE. If the given entity instance is managed, that is, if it belongs to this persistence context, and has not been marked for removal, it is itself ignored, but the operation still cascades, and it is returned directly.- Parameters:
entity- a new, managed, or detached entity instance
- Returns:
- the managed instance that the state was merged to.
- Throws:
- if the instance is not an entity or is a removed entity.IllegalArgumentException- if there is no transaction when invoked on a container-managed entity manager of that is of typeTransactionRequiredExceptionPersistenceContextType.TRANSACTION.
- Since:
- Jakarta Persistence (JPA) 1.0
cascade=PERSIST. If the given entity instance is already managed, that is, if it already belongs to this persistence context, and has not been marked for removal, it is itself ignored, but the operation still cascades.- Parameters:
entity- a new, managed, or removed entity instance
- Throws:
- if the given instance is not an entity.IllegalArgumentException- if the given entity is detached (if the entity is detached, theEntityExistsExceptionEntityExistsExceptionmay be thrown when the persist operation is invoked, or theEntityExistsExceptionor anotherPersistenceExceptionmay be thrown at flush or commit time).- if there is no transaction when invoked on a container-managed entity manager that is of typeTransactionRequiredExceptionPersistenceContextType.TRANSACTION.
- Since:
- Jakarta Persistence (JPA) 1.0
cascade=REFRESH.- Parameters:
entity- a managed entity instance
- Throws:
- if the entity no longer exists in the database.EntityNotFoundException- if the instance is not an entity or the entity is not managed.IllegalArgumentException- if there is no transaction when invoked on a container-managed entity manager of typeTransactionRequiredExceptionPersistenceContextType.TRANSACTION.
- Since:
- Jakarta Persistence (JPA) 1.0
cascade=REFRESH. 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:
- if the entity no longer exists in the database.EntityNotFoundException- if the instance is not an entity or the entity is not managed.IllegalArgumentException- if there is no transaction when invoked on a container-managed entity manager of typeTransactionRequiredExceptionPersistenceContextType.TRANSACTION.
- Since:
- Jakarta Persistence (JPA) 2.0
cascade=REFRESH. 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.
- Parameters:
lockMode- lock modeentity- a managed entity instance
- Throws:
- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if the entity no longer exists in the database.EntityNotFoundException- if the instance is not an entity or if the entity is not managed.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if invoked on a container-managed entity manager of typeTransactionRequiredExceptionPersistenceContextType.TRANSACTIONwhen there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other thanLockModeType.NONEwas specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other thanNONEwas specified.
- Since:
- Jakarta Persistence (JPA) 2.0
cascade=REFRESH. 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 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:
lockMode- lock modeentity- a managed entity instanceproperties- standard and vendor-specific properties and hints
- Throws:
- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if the entity no longer exists in the database.EntityNotFoundException- if the instance is not an entity or if the entity is not managed.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if invoked on a container-managed entity manager of typeTransactionRequiredExceptionPersistenceContextType.TRANSACTIONwhen there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other thanLockModeType.NONEwas specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other thanNONEwas specified.
- Since:
- Jakarta Persistence (JPA) 2.0
LockModeType, lock the given entity, obtaining the given lock mode. This operation cascades to every entity related by an association marked cascade=REFRESH. 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 RefreshOption is not recognized, it is silently ignored.
Portable applications should not rely on the standard timeout option. Depending on the database in use and the locking mechanisms used by the provider, the hint may or may not be observed.
- Parameters:
options- standard and vendor-specific optionsentity- a managed entity instance
- Throws:
- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if the entity no longer exists in the database.EntityNotFoundException- if the instance is not an entity or if the entity is not managed.IllegalArgumentException- if an unsupported lock call is made.PersistenceException- if invoked on a container-managed entity manager of typeTransactionRequiredExceptionPersistenceContextType.TRANSACTIONwhen there is no transaction; if invoked on an extended entity manager when there is no transaction and a lock mode other thanLockModeType.NONEwas specified; or if invoked on an extended entity manager that has not been joined to the current transaction and any lock mode other thanNONEwas specified.
- Since:
- Jakarta Persistence (JPA) 3.2
cascade=REMOVE. If the given entity instance is already removed, it is ignored. If the given entity is new, it is itself ignored, but the operation still cascades.- Parameters:
entity- a managed, new, or removed entity instance
- Throws:
- if the instance is not an entity or is a detached entity.IllegalArgumentException- if invoked on a container-managed entity manager of typeTransactionRequiredExceptionPersistenceContextType.TRANSACTIONand there is no transaction.
- Since:
- Jakarta Persistence (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
- Throws:
- wrapping the checkedPersistenceExceptionExceptionthrown byConnectionConsumer.accept, if any.
- Since:
- Jakarta Persistence (JPA) 3.2
- Parameters:
cacheRetrieveMode- cache retrieval mode
- Since:
- Jakarta Persistence (JPA) 3.2
- Parameters:
cacheStoreMode- cache storage mode
- Since:
- Jakarta Persistence (JPA) 3.2
- Parameters:
flushMode- flush mode
- Since:
- Jakarta Persistence (JPA) 1.0
- Parameters:
propertyName- name of the property or hintvalue- value for the property or hint
- Throws:
- if the property or hint name is recognized by the implementation, but the second argument is not valid value.IllegalArgumentException
- Since:
- Jakarta Persistence (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 implementingEntityManageror an interface it implements.
- Returns:
- an instance of the specified class.
- Throws:
- if the provider does not support the given type.PersistenceException
- Since:
- Jakarta Persistence (JPA) 2.0