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 |
|---|---|
| public void | |
| public void | |
| public boolean | |
| public <T> EntityGraph | Returns: entity graphclass of entity graph rootType)Return a mutable EntityGraph that can be used to dynamically create an EntityGraph. |
| public EntityGraph | Returns: entity graphname of an entity graph graphName)Return a mutable copy of the named EntityGraph. |
| public Query | Returns: the new query instancethe name of a query defined in metadata name)Create an instance of |
| public <T> TypedQuery | Returns: the new query instancethe name of a query defined in metadata name, Class<T> the type of the query result resultClass)Create an instance of |
| public StoredProcedureQuery | Returns: the new stored procedure query instancename assigned to the stored procedure query
in metadata name)Create an instance of |
| public Query | Returns: the new query instancea native SQL query string sqlString)Create an instance of |
| public Query | Returns: the new query instancea native SQL query string sqlString, Class<T> the class of the resulting instance(s) resultClass)Create an instance of |
| public Query | Returns: the new query instancea native SQL query string sqlString, String the name of the result set mapping resultSetMapping)Create an instance of |
| public Query | Returns: the new query instancea Java Persistence query string qlString)Create an instance of |
| public <T> TypedQuery | Returns: the new query instancea criteria query object criteriaQuery)Create an instance of |
| public Query | Returns: the new query instancea criteria update query object updateQuery)Create an instance of |
| public Query | Returns: the new query instancea criteria delete query object deleteQuery)Create an instance of |
| public <T> TypedQuery | Returns: the new query instancea Java Persistence query string qlString, Class<T> the type of the query result resultClass)Create an instance of |
| public StoredProcedureQuery | Returns: the new stored procedure query instancename of the stored procedure in the
database procedureName)Create an instance of |
| public StoredProcedureQuery | Returns: the new stored procedure query instancename of the stored procedure in the
database procedureName, Class<T>... classes to which the result sets
produced by the stored procedure are to
be mapped resultClasses)Create an instance of |
| public StoredProcedureQuery | Returns: the new stored procedure query instancename of the stored procedure in the
database procedureName, String... the names of the result set mappings
to be used in mapping result sets
returned by the stored procedure resultSetMappings)Create an instance of |
| public void | |
| public <T> T | |
| public <T> T | |
| public <T> T | Returns: the found entity instance or null if the entity does not existentity class entityClass, Object primary key primaryKey, LockModeType lock mode lockMode)Find by primary key and lock. |
| public <T> T | Returns: the found entity instance or null if the entity does not existentity class entityClass, Object primary key primaryKey, LockModeType lock mode lockMode, Map<String, Object> standard and vendor-specific properties
and hints properties)Find by primary key and lock, using the specified properties. |
| public void | |
| public CriteriaBuilder | Returns: CriteriaBuilder instanceReturn an instance of |
| public Object | Returns: underlying provider object for EntityManagerReturn the underlying provider object for the |
| public EntityGraph | Returns: named entity graphname of an existing entity graph graphName)Return a named EntityGraph. |
| public <T> List | Returns: list of all entity graphs defined for the entityentity class entityClass)Return all named EntityGraphs that have been defined for the provided class type. |
| public EntityManagerFactory | Returns: EntityManagerFactory instanceReturn the entity manager factory for the entity manager. |
| public FlushModeType | Returns: flushModeGet the flush mode that applies to all objects contained in the persistence context. |
| public LockModeType | Returns: lock modeentity instance entity)Get the current lock mode for the entity instance. |
| public Metamodel | Returns: Metamodel instanceReturn an instance of |
| public Map | Returns: map of properties and hints in effect for entity managerGet the properties and hints and associated values that are in effect for the entity manager. |
| public <T> T | Returns: the found entity instanceentity class entityClass, Object primary key primaryKey)Get an instance, whose state may be lazily fetched. |
| public EntityTransaction | Returns: EntityTransaction instanceReturn the resource-level |
| public boolean | Returns: booleanDetermine whether the entity manager is joined to the current transaction. |
| public boolean | Returns: true until the entity manager has been closedDetermine whether the entity manager is open. |
| public void | joinTransaction()
Indicate to the entity manager that a JTA transaction is active and join the persistence context to it. |
| public void | lock(Object
entity instance entity, LockModeType lock mode lockMode)Lock an entity instance that is contained in the persistence context with the specified lock mode type. |
| public void | |
| public <T> T | Returns: the managed instance that the state was merged toentity instance entity)Merge the state of the given entity into the current persistence context. |
| public void | |
| public void | |
| public void | |
| public void | refresh(Object
entity instance entity, LockModeType lock mode 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. |
| public void | refresh(Object
entity instance entity, LockModeType lock mode lockMode, Map<String, Object> standard and vendor-specific properties
and hints 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. |
| public void | |
| public void | setFlushMode(FlushModeType
flush mode flushMode)Set the flush mode that applies to all objects contained in the persistence context. |
| public void | setProperty(String
name of property or hint propertyName, Object value for property or hint value)Set an entity manager property or hint. |
| public <T> T |
| clear | back to summary |
|---|---|
| public void clear() Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted. | |
| close | back to summary |
|---|---|
| public void close() Close an application-managed entity manager.
After the close method has been invoked, all methods
on the
| |
| contains | back to summary |
|---|---|
| public boolean contains(Object entity) Check if the instance is a managed entity instance belonging to the current persistence context.
| |
| createEntityGraph | back to summary |
|---|---|
| public <T> EntityGraph Return a mutable EntityGraph that can be used to dynamically create an EntityGraph.
| |
| createEntityGraph | back to summary |
|---|---|
| public EntityGraph Return a mutable copy of the named EntityGraph. If there is no entity graph with the specified name, null is returned.
| |
| createNamedQuery | back to summary |
|---|---|
| public Query createNamedQuery(String name) Create an instance of
| |
| createNamedQuery | back to summary |
|---|---|
| public <T> TypedQuery Create an instance of
| |
| createNamedStoredProcedureQuery | back to summary |
|---|---|
| public StoredProcedureQuery createNamedStoredProcedureQuery(String name) Create an instance of 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[].
| |
| createNativeQuery | back to summary |
|---|---|
| public Query createNativeQuery(String sqlString) Create an instance of | |
| createNativeQuery | back to summary |
|---|---|
| public Query createNativeQuery(String sqlString, Class<T> resultClass) Create an instance of | |
| createNativeQuery | back to summary |
|---|---|
| public Query createNativeQuery(String sqlString, String resultSetMapping) Create an instance of | |
| createQuery | back to summary |
|---|---|
| public Query createQuery(String qlString) Create an instance of
| |
| createQuery | back to summary |
|---|---|
| public <T> TypedQuery Create an instance of
| |
| createQuery | back to summary |
|---|---|
| public Query createQuery(CriteriaUpdate<T> updateQuery) Create an instance of
| |
| createQuery | back to summary |
|---|---|
| public Query createQuery(CriteriaDelete<T> deleteQuery) Create an instance of
| |
| createQuery | back to summary |
|---|---|
| public <T> TypedQuery Create an instance of
| |
| createStoredProcedureQuery | back to summary |
|---|---|
| public StoredProcedureQuery createStoredProcedureQuery(String procedureName) Create an instance of 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[].
| |
| createStoredProcedureQuery | back to summary |
|---|---|
| public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class<T>... resultClasses) Create an instance of Parameters must be registered before the stored procedure can be executed. The
| |
| createStoredProcedureQuery | back to summary |
|---|---|
| public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings) Create an instance of Parameters must be registered before the stored procedure can be executed. The
| |
| detach | back to summary |
|---|---|
| public void detach(Object entity) Remove the given entity from the persistence context, causing a managed entity to become detached. Unflushed changes made to the entity if any (including removal of the entity), will not be synchronized to the database. Entities which previously referenced the detached entity will continue to reference it.
| |
| find | back to summary |
|---|---|
| public <T> T find(Class<T> entityClass, Object primaryKey) Find by primary key. Search for an entity of the specified class and primary key. If the entity instance is contained in the persistence context, it is returned from there.
| |
| find | back to summary |
|---|---|
| public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties) Find by primary key, using the specified properties. Search for an entity of the specified class and primary key. If the entity instance is contained in the persistence context, it is returned from there. If a vendor-specific property or hint is not recognized, it is silently ignored.
| |
| find | back to summary |
|---|---|
| public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) Find by primary key and lock. Search for an entity of the specified class and primary key and lock it with respect to the specified lock type. If the entity instance is contained in the persistence context, it is returned from there, and the effect of this method is the same as if the lock method had been called on the entity. If the entity is found within the persistence context and the
lock mode type is pessimistic and the entity has a version
attribute, the persistence provider must perform optimistic
version checks when obtaining the database lock. If these
checks fail, the If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
| |
| find | back to summary |
|---|---|
| public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties) Find by primary key and lock, using the specified properties. Search for an entity of the specified class and primary key and lock it with respect to the specified lock type. If the entity instance is contained in the persistence context, it is returned from there. If the entity is found
within the persistence context and the lock mode type
is pessimistic and the entity has a version attribute, the
persistence provider must perform optimistic version checks
when obtaining the database lock. If these checks fail,
the If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
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.
| |
| flush | back to summary |
|---|---|
| public void flush() Synchronize the persistence context to the underlying database.
| |
| getCriteriaBuilder | back to summary |
|---|---|
| public CriteriaBuilder getCriteriaBuilder() Return an instance of
| |
| getDelegate | back to summary |
|---|---|
| public Object getDelegate() Return the underlying provider object for the The
| |
| getEntityGraph | back to summary |
|---|---|
| public EntityGraph Return a named EntityGraph. The returned EntityGraph should be considered immutable.
| |
| getEntityGraphs | back to summary |
|---|---|
| public <T> List Return all named EntityGraphs that have been defined for the provided class type.
| |
| getEntityManagerFactory | back to summary |
|---|---|
| public EntityManagerFactory getEntityManagerFactory() Return the entity manager factory for the entity manager.
| |
| getFlushMode | back to summary |
|---|---|
| public FlushModeType getFlushMode() Get the flush mode that applies to all objects contained in the persistence context.
| |
| getLockMode | back to summary |
|---|---|
| public LockModeType getLockMode(Object entity) Get the current lock mode for the entity instance.
| |
| getMetamodel | back to summary |
|---|---|
| public Metamodel getMetamodel() Return an instance of
| |
| getProperties | back to summary |
|---|---|
| public Map Get the properties and hints and associated values that are in effect for the entity manager. Changing the contents of the map does not change the configuration in effect. | |
| getReference | back to summary |
|---|---|
| public <T> T getReference(Class<T> entityClass, Object primaryKey) Get an instance, whose state may be lazily fetched.
If the requested instance does not exist in the database,
the
| |
| getTransaction | back to summary |
|---|---|
| public EntityTransaction getTransaction() Return the resource-level
| |
| isJoinedToTransaction | back to summary |
|---|---|
| public boolean isJoinedToTransaction() Determine whether the entity manager is joined to the current transaction. Returns false if the entity manager is not joined to the current transaction or if no transaction is active
| |
| isOpen | back to summary |
|---|---|
| public boolean isOpen() Determine whether the entity manager is open.
| |
| joinTransaction | back to summary |
|---|---|
| public void joinTransaction() Indicate to the entity manager that a JTA transaction is active and join the persistence context to it. 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
| |
| lock | back to summary |
|---|---|
| public void lock(Object entity, LockModeType lockMode) Lock an entity instance that is contained in the persistence context with the specified lock mode type. If a pessimistic lock mode type is specified and the entity
contains a version attribute, the persistence provider must
also perform optimistic version checks when obtaining the
database lock. If these checks fail, the
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
| |
| lock | back to summary |
|---|---|
| public 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. 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
If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
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.
| |
| merge | back to summary |
|---|---|
| public <T> T merge(T entity) Merge the state of the given entity into the current persistence context.
| |
| persist | back to summary |
|---|---|
| public void persist(Object entity) Make an instance managed and persistent.
| |
| refresh | back to summary |
|---|---|
| public void refresh(Object entity) Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
| |
| refresh | back to summary |
|---|---|
| public 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. If a vendor-specific property or hint is not recognized, it is silently ignored.
| |
| refresh | back to summary |
|---|---|
| public 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. If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
| |
| refresh | back to summary |
|---|---|
| public 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. If the lock mode type is pessimistic and the entity instance is found but cannot be locked:
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.
| |
| remove | back to summary |
|---|---|
| public void remove(Object entity) Remove the entity instance.
| |
| setFlushMode | back to summary |
|---|---|
| public void setFlushMode(FlushModeType flushMode) Set the flush mode that applies to all objects contained in the persistence context.
| |
| setProperty | back to summary |
|---|---|
| public void setProperty(String propertyName, Object value) Set an entity manager property or hint. If a vendor-specific property or hint is not recognized, it is silently ignored.
| |
| unwrap | back to summary |
|---|---|
| public <T> T unwrap(Class<T> cls) Return an object of the specified type to allow access to the
provider-specific API. If the provider's
| |