public interface 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.
Query
,
TypedQuery
,
CriteriaQuery
,
PersistenceContext
,
StoredProcedureQuery
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear the persistence context, causing all managed
entities to become detached.
|
void |
close()
Close an application-managed entity manager.
|
boolean |
contains(Object entity)
Check if the instance is a managed entity instance belonging
to the current persistence context.
|
<T> EntityGraph<T> |
createEntityGraph(Class<T> rootType)
Return a mutable EntityGraph that can be used to dynamically create an
EntityGraph.
|
EntityGraph<?> |
createEntityGraph(String graphName)
Return a mutable copy of the named EntityGraph.
|
Query |
createNamedQuery(String name)
Create an instance of
Query for executing a named query
(in the Jakarta Persistence query language or in native SQL). |
<T> TypedQuery<T> |
createNamedQuery(String name,
Class<T> resultClass)
Create an instance of
TypedQuery for executing a
Jakarta Persistence query language named query. |
StoredProcedureQuery |
createNamedStoredProcedureQuery(String name)
Create an instance of
StoredProcedureQuery for executing a
stored procedure in the database. |
Query |
createNativeQuery(String sqlString)
Create an instance of
Query for executing
a native SQL statement, e.g., for update or delete. |
Query |
createNativeQuery(String sqlString,
Class resultClass)
Create an instance of
Query for executing
a native SQL query. |
Query |
createNativeQuery(String sqlString,
String resultSetMapping)
Create an instance of
Query for executing
a native SQL query. |
Query |
createQuery(CriteriaDelete deleteQuery)
Create an instance of
Query for executing a criteria
delete query. |
<T> TypedQuery<T> |
createQuery(CriteriaQuery<T> criteriaQuery)
Create an instance of
TypedQuery for executing a
criteria query. |
Query |
createQuery(CriteriaUpdate updateQuery)
Create an instance of
Query for executing a criteria
update query. |
Query |
createQuery(String qlString)
Create an instance of
Query for executing a
Jakarta Persistence query language statement. |
<T> TypedQuery<T> |
createQuery(String qlString,
Class<T> resultClass)
Create an instance of
TypedQuery for executing a
Jakarta Persistence query language statement. |
StoredProcedureQuery |
createStoredProcedureQuery(String procedureName)
Create an instance of
StoredProcedureQuery for executing a
stored procedure in the database. |
StoredProcedureQuery |
createStoredProcedureQuery(String procedureName,
Class... resultClasses)
Create an instance of
StoredProcedureQuery for executing a
stored procedure in the database. |
StoredProcedureQuery |
createStoredProcedureQuery(String procedureName,
String... resultSetMappings)
Create an instance of
StoredProcedureQuery for executing a
stored procedure in the database. |
void |
detach(Object entity)
Remove the given entity from the persistence context, causing
a managed entity to become detached.
|
<T> T |
find(Class<T> entityClass,
Object primaryKey)
Find by primary key.
|
<T> T |
find(Class<T> entityClass,
Object primaryKey,
LockModeType lockMode)
Find by primary key and lock.
|
<T> T |
find(Class<T> entityClass,
Object primaryKey,
LockModeType lockMode,
Map<String,Object> properties)
Find by primary key and lock, using the specified properties.
|
<T> T |
find(Class<T> entityClass,
Object primaryKey,
Map<String,Object> properties)
Find by primary key, using the specified properties.
|
void |
flush()
Synchronize the persistence context to the
underlying database.
|
CriteriaBuilder |
getCriteriaBuilder()
Return an instance of
CriteriaBuilder for the creation of
CriteriaQuery objects. |
Object |
getDelegate()
Return the underlying provider object for the
EntityManager ,
if available. |
EntityGraph<?> |
getEntityGraph(String graphName)
Return a named EntityGraph.
|
<T> List<EntityGraph<? super T>> |
getEntityGraphs(Class<T> entityClass)
Return all named EntityGraphs that have been defined for the provided
class type.
|
EntityManagerFactory |
getEntityManagerFactory()
Return the entity manager factory for the entity manager.
|
FlushModeType |
getFlushMode()
Get the flush mode that applies to all objects contained
in the persistence context.
|
LockModeType |
getLockMode(Object entity)
Get the current lock mode for the entity instance.
|
Metamodel |
getMetamodel()
Return an instance of
Metamodel interface for access to the
metamodel of the persistence unit. |
Map<String,Object> |
getProperties()
Get the properties and hints and associated values that are in effect
for the entity manager.
|
<T> T |
getReference(Class<T> entityClass,
Object primaryKey)
Get an instance, whose state may be lazily fetched.
|
EntityTransaction |
getTransaction()
Return the resource-level
EntityTransaction object. |
boolean |
isJoinedToTransaction()
Determine whether the entity manager is joined to the
current transaction.
|
boolean |
isOpen()
Determine whether the entity manager is open.
|
void |
joinTransaction()
Indicate to the entity manager that a JTA transaction is
active and join the persistence context to it.
|
void |
lock(Object entity,
LockModeType lockMode)
Lock an entity instance that is contained in the persistence
context with the specified lock mode type.
|
void |
lock(Object entity,
LockModeType lockMode,
Map<String,Object> properties)
Lock an entity instance that is contained in the persistence
context with the specified lock mode type and with specified
properties.
|
<T> T |
merge(T entity)
Merge the state of the given entity into the
current persistence context.
|
void |
persist(Object entity)
Make an instance managed and persistent.
|
void |
refresh(Object entity)
Refresh the state of the instance from the database,
overwriting changes made to the entity, if any.
|
void |
refresh(Object entity,
LockModeType lockMode)
Refresh the state of the instance from the database,
overwriting changes made to the entity, if any, and
lock it with respect to given lock mode type.
|
void |
refresh(Object entity,
LockModeType lockMode,
Map<String,Object> properties)
Refresh the state of the instance from the database,
overwriting changes made to the entity, if any, and
lock it with respect to given lock mode type and with
specified properties.
|
void |
refresh(Object entity,
Map<String,Object> properties)
Refresh the state of the instance from the database, using
the specified properties, and overwriting changes made to
the entity, if any.
|
void |
remove(Object entity)
Remove the entity instance.
|
void |
setFlushMode(FlushModeType flushMode)
Set the flush mode that applies to all objects contained
in the persistence context.
|
void |
setProperty(String propertyName,
Object value)
Set an entity manager property or hint.
|
<T> T |
unwrap(Class<T> cls)
Return an object of the specified type to allow access to the
provider-specific API.
|
void persist(Object entity)
entity
- entity instanceEntityExistsException
- if the entity already exists.
(If the entity already exists, the EntityExistsException
may
be thrown when the persist operation is invoked, or the
EntityExistsException
or another PersistenceException
may be
thrown at flush or commit time.)IllegalArgumentException
- if the instance is not an
entityTransactionRequiredException
- if there is no transaction when
invoked on a container-managed entity manager of that is of type
PersistenceContextType.TRANSACTION
<T> T merge(T entity)
entity
- entity instanceIllegalArgumentException
- if instance is not an
entity or is a removed entityTransactionRequiredException
- if there is no transaction when
invoked on a container-managed entity manager of that is of type
PersistenceContextType.TRANSACTION
void remove(Object entity)
entity
- entity instanceIllegalArgumentException
- if the instance is not an
entity or is a detached entityTransactionRequiredException
- if invoked on a
container-managed entity manager of type
PersistenceContextType.TRANSACTION
and there is
no transaction<T> T find(Class<T> entityClass, Object primaryKey)
entityClass
- entity classprimaryKey
- primary keyIllegalArgumentException
- 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<T> T find(Class<T> entityClass, Object primaryKey, Map<String,Object> properties)
entityClass
- entity classprimaryKey
- primary keyproperties
- standard and vendor-specific properties
and hintsIllegalArgumentException
- 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<T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode)
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:
PessimisticLockException
will be thrown if the database
locking failure causes transaction-level rollback
LockTimeoutException
will be thrown if the database
locking failure causes only statement-level rollback
entityClass
- entity classprimaryKey
- primary keylockMode
- lock modeIllegalArgumentException
- 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 nullTransactionRequiredException
- 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 than NONE
is specifiedOptimisticLockException
- if the optimistic version
check failsPessimisticLockException
- if pessimistic locking
fails and the transaction is rolled backLockTimeoutException
- if pessimistic locking fails and
only the statement is rolled backPersistenceException
- if an unsupported lock call
is made<T> 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
will be thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
PessimisticLockException
will be thrown if the database
locking failure causes transaction-level rollback
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.
entityClass
- entity classprimaryKey
- primary keylockMode
- lock modeproperties
- standard and vendor-specific properties
and hintsIllegalArgumentException
- 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 nullTransactionRequiredException
- 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 than NONE
is specifiedOptimisticLockException
- if the optimistic version
check failsPessimisticLockException
- if pessimistic locking
fails and the transaction is rolled backLockTimeoutException
- if pessimistic locking fails and
only the statement is rolled backPersistenceException
- if an unsupported lock call
is made<T> T getReference(Class<T> entityClass, Object primaryKey)
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.entityClass
- entity classprimaryKey
- primary keyIllegalArgumentException
- 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 nullEntityNotFoundException
- if the entity state
cannot be accessedvoid flush()
TransactionRequiredException
- if there is
no transaction or if the entity manager has not been
joined to the current transactionPersistenceException
- if the flush failsvoid setFlushMode(FlushModeType flushMode)
flushMode
- flush modeFlushModeType getFlushMode()
void lock(Object entity, LockModeType lockMode)
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:
PessimisticLockException
will be thrown if the database
locking failure causes transaction-level rollback
LockTimeoutException
will be thrown if the database
locking failure causes only statement-level rollback
entity
- entity instancelockMode
- lock modeIllegalArgumentException
- if the instance is not an
entity or is a detached entityTransactionRequiredException
- if there is no
transaction or if invoked on an entity manager which
has not been joined to the current transactionEntityNotFoundException
- if the entity does not exist
in the database when pessimistic locking is
performedOptimisticLockException
- if the optimistic version
check failsPessimisticLockException
- if pessimistic locking fails
and the transaction is rolled backLockTimeoutException
- if pessimistic locking fails and
only the statement is rolled backPersistenceException
- if an unsupported lock call
is madevoid lock(Object entity, LockModeType lockMode, Map<String,Object> properties)
If a pessimistic lock mode type is specified and the entity
contains a version attribute, the persistence provider must
also perform optimistic version checks when obtaining the
database lock. If these checks fail, the
OptimisticLockException
will be thrown.
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
PessimisticLockException
will be thrown if the database
locking failure causes transaction-level rollback
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.
entity
- entity instancelockMode
- lock modeproperties
- standard and vendor-specific properties
and hintsIllegalArgumentException
- if the instance is not an
entity or is a detached entityTransactionRequiredException
- if there is no
transaction or if invoked on an entity manager which
has not been joined to the current transactionEntityNotFoundException
- if the entity does not exist
in the database when pessimistic locking is
performedOptimisticLockException
- if the optimistic version
check failsPessimisticLockException
- if pessimistic locking fails
and the transaction is rolled backLockTimeoutException
- if pessimistic locking fails and
only the statement is rolled backPersistenceException
- if an unsupported lock call
is madevoid refresh(Object entity)
entity
- entity instanceIllegalArgumentException
- if the instance is not
an entity or the entity is not managedTransactionRequiredException
- 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 databasevoid refresh(Object entity, Map<String,Object> properties)
If a vendor-specific property or hint is not recognized, it is silently ignored.
entity
- entity instanceproperties
- standard and vendor-specific properties
and hintsIllegalArgumentException
- if the instance is not
an entity or the entity is not managedTransactionRequiredException
- 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 databasevoid refresh(Object entity, LockModeType lockMode)
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
PessimisticLockException
will be thrown if the database
locking failure causes transaction-level rollback
LockTimeoutException
will be thrown if the
database locking failure causes only statement-level
rollback.
entity
- entity instancelockMode
- lock modeIllegalArgumentException
- if the instance is not
an entity or the entity is not managedTransactionRequiredException
- 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 than NONE
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 than NONE
has been specifiedEntityNotFoundException
- if the entity no longer exists
in the databasePessimisticLockException
- if pessimistic locking fails
and the transaction is rolled backLockTimeoutException
- if pessimistic locking fails and
only the statement is rolled backPersistenceException
- if an unsupported lock call
is madevoid refresh(Object entity, LockModeType lockMode, Map<String,Object> properties)
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
PessimisticLockException
will be thrown if the database
locking failure causes transaction-level rollback
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.
entity
- entity instancelockMode
- lock modeproperties
- standard and vendor-specific properties
and hintsIllegalArgumentException
- if the instance is not
an entity or the entity is not managedTransactionRequiredException
- 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 than NONE
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 than NONE
has been specifiedEntityNotFoundException
- if the entity no longer exists
in the databasePessimisticLockException
- if pessimistic locking fails
and the transaction is rolled backLockTimeoutException
- if pessimistic locking fails and
only the statement is rolled backPersistenceException
- if an unsupported lock call
is madevoid clear()
void detach(Object entity)
entity
- entity instanceIllegalArgumentException
- if the instance is not an
entityboolean contains(Object entity)
entity
- entity instanceIllegalArgumentException
- if not an entityLockModeType getLockMode(Object entity)
entity
- entity instanceTransactionRequiredException
- if there is no
transaction or if the entity manager has not been
joined to the current transactionIllegalArgumentException
- if the instance is not a
managed entity and a transaction is activevoid setProperty(String propertyName, Object value)
propertyName
- name of property or hintvalue
- value for property or hintIllegalArgumentException
- if the second argument is
not valid for the implementationMap<String,Object> getProperties()
Query createQuery(String qlString)
Query
for executing a
Jakarta Persistence query language statement.qlString
- a Jakarta Persistence query stringIllegalArgumentException
- if the query string is
found to be invalid<T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery)
TypedQuery
for executing a
criteria query.criteriaQuery
- a criteria query objectIllegalArgumentException
- if the criteria query is
found to be invalidQuery createQuery(CriteriaUpdate updateQuery)
Query
for executing a criteria
update query.updateQuery
- a criteria update query objectIllegalArgumentException
- if the update query is
found to be invalidQuery createQuery(CriteriaDelete deleteQuery)
Query
for executing a criteria
delete query.deleteQuery
- a criteria delete query objectIllegalArgumentException
- if the delete query is
found to be invalid<T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass)
TypedQuery
for executing a
Jakarta Persistence query language statement.
The select list of the query must contain only a single
item, which must be assignable to the type specified by
the resultClass
argument.qlString
- a Jakarta Persistence query stringresultClass
- the type of the query resultIllegalArgumentException
- if the query string is found
to be invalid or if the query result is found to
not be assignable to the specified typeQuery createNamedQuery(String name)
Query
for executing a named query
(in the Jakarta Persistence query language or in native SQL).name
- the name of a query defined in metadataIllegalArgumentException
- if a query has not been
defined with the given name or if the query string is
found to be invalid<T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass)
TypedQuery
for executing a
Jakarta Persistence query language named query.
The select list of the query must contain only a single
item, which must be assignable to the type specified by
the resultClass
argument.name
- the name of a query defined in metadataresultClass
- the type of the query resultIllegalArgumentException
- 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 typeQuery createNativeQuery(String sqlString)
Query
for executing
a native SQL statement, e.g., for update or delete.
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
appearance in the select list and default JDBC type
mappings are applied.sqlString
- a native SQL query stringQuery createNativeQuery(String sqlString, Class resultClass)
Query
for executing
a native SQL query.sqlString
- a native SQL query stringresultClass
- the class of the resulting instance(s)Query createNativeQuery(String sqlString, String resultSetMapping)
Query
for executing
a native SQL query.sqlString
- a native SQL query stringresultSetMapping
- the name of the result set mappingStoredProcedureQuery createNamedStoredProcedureQuery(String name)
StoredProcedureQuery
for executing a
stored procedure in the database.
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[].
name
- name assigned to the stored procedure query
in metadataIllegalArgumentException
- if a query has not been
defined with the given nameStoredProcedureQuery createStoredProcedureQuery(String procedureName)
StoredProcedureQuery
for executing a
stored procedure in the database.
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[].
procedureName
- name of the stored procedure in the
databaseIllegalArgumentException
- if a stored procedure of the
given name does not exist (or the query execution will
fail)StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses)
StoredProcedureQuery
for executing a
stored procedure in the database.
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.
procedureName
- name of the stored procedure in the
databaseresultClasses
- classes to which the result sets
produced by the stored procedure are to
be mappedIllegalArgumentException
- if a stored procedure of the
given name does not exist (or the query execution will
fail)StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings)
StoredProcedureQuery
for executing a
stored procedure in the database.
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.
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 procedureIllegalArgumentException
- if a stored procedure or
result set mapping of the given name does not exist
(or the query execution will fail)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.
TransactionRequiredException
- if there is
no transactionboolean isJoinedToTransaction()
<T> T unwrap(Class<T> cls)
EntityManager
implementation does not support the specified class, the
PersistenceException
is thrown.cls
- the class of the object to be returned. This is
normally either the underlying EntityManager
implementation
class or an interface that it implements.PersistenceException
- if the provider does not
support the callObject getDelegate()
EntityManager
,
if available. The result of this method is implementation
specific.
The unwrap
method is to be preferred for new applications.
void close()
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.IllegalStateException
- if the entity manager
is container-managedboolean isOpen()
EntityTransaction getTransaction()
EntityTransaction
object.
The EntityTransaction
instance may be used serially to
begin and commit multiple transactions.IllegalStateException
- if invoked on a JTA
entity managerEntityManagerFactory getEntityManagerFactory()
IllegalStateException
- if the entity manager has
been closedCriteriaBuilder getCriteriaBuilder()
CriteriaBuilder
for the creation of
CriteriaQuery
objects.IllegalStateException
- if the entity manager has
been closedMetamodel getMetamodel()
Metamodel
interface for access to the
metamodel of the persistence unit.IllegalStateException
- if the entity manager has
been closed<T> EntityGraph<T> createEntityGraph(Class<T> rootType)
rootType
- class of entity graphEntityGraph<?> createEntityGraph(String graphName)
graphName
- name of an entity graphEntityGraph<?> getEntityGraph(String graphName)
graphName
- name of an existing entity graphIllegalArgumentException
- if there is no EntityGraph of
the given name<T> List<EntityGraph<? super T>> getEntityGraphs(Class<T> entityClass)
entityClass
- entity classIllegalArgumentException
- if the class is not an entityCopyright © 2019 JBoss by Red Hat. All rights reserved.