EntityManager.
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. If two entity types participate in an association, then they must belong to the same persistence unit.
A persistence unit may be defined by a persistence.xml
file, or it may be defined at runtime via the
PersistenceConfiguration API.
Every persistence unit has a transaction type,
either JTA, or
RESOURCE_LOCAL.
Resource-local transactions are managed programmatically via the
EntityTransaction interface.
An EntityManagerFactory with a lifecycle managed by
the application may be created using the static operations of
the Persistence class:
persistence.xml,
an entity manager factory may be created by calling
Persistence#createEntityManagerFactory(String) or
Persistence#createEntityManagerFactory(String, Map),
or
PersistenceConfiguration, an entity manager factory
may be created by calling
Persistence#createEntityManagerFactory(PersistenceConfiguration).
Usually, there is exactly one EntityManagerFactory for
each persistence unit:
// create a factory at initialization time
static final EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("orderMgt");
Alternatively, in the Jakarta EE environment, a
container-managed EntityManagerFactory may be obtained
by dependency injection, using PersistenceUnit.
// inject the container-managed factory
@PersistenceUnit(unitName="orderMgt")
EntityManagerFactory entityManagerFactory;
An application-managed EntityManager may be created
via a call to createEntityManager(). However, this
approach places complete responsibility for cleanup and exception
management on the client, and is thus considered error-prone. It
is much safer to use the methods runInTransaction and
callInTransaction to obtain EntityManagers.
Alternatively, in the Jakarta EE environment, a container-managed
EntityManager may be obtained by dependency injection,
using PersistenceContext, and the application need not
interact with the EntityManagerFactory directly.
The EntityManagerFactory provides access to certain
other useful APIs:
Metamodel exposing a model of the
managed types associated with the persistence unit may be
obtained by calling getMetamodel(),
SchemaManager, allowing programmatic
control over schema generation and validation, may be
obtained by calling getSchemaManager(),
Cache, allowing direct programmatic
control over the second-level cache, may be obtained by
calling getCache(),
CriteriaBuilder, used to define criteria queries,
may be obtained by calling getCriteriaBuilder(),
and
PersistenceUnitUtil may be obtained by calling
getPersistenceUnitUtil().
When the application has finished using the entity manager
factory, or when the application terminates, the application
should close the entity manager factory. If
necessary, a java. may be used:
// factory should be destroyed before program terminates
Cleaner.create().register(entityManagerFactory, entityManagerFactory::close);
EntityManagerFactory has been closed, all its
entity managers are considered to be in the closed state.
EntityManager
| Modifier and Type | Method and Description |
|---|---|
| public <T> void | addNamedEntityGraph(String
name for the entity graph graphName, EntityGraph<T> entity graph entityGraph)Add a named copy of the given |
| public void | addNamedQuery(String
name for the query name, Query query)Define the query, typed query, or stored procedure query as
a named query such that future query objects can be created
from it using the |
| public <R> R | Returns: the value returned by the given functiona function to be called in the scope of the transaction work)Create a new application-managed |
| public void | close()
Redeclares java. Close the factory, releasing any resources that it holds. |
| public EntityManager | Returns: entity manager instanceCreate a new application-managed |
| public EntityManager | Returns: entity manager instanceproperties for entity manager map)Create a new application-managed |
| public EntityManager | Returns: entity manager instancehow and when the entity manager should
be synchronized with the current JTA
transaction synchronizationType)Create a new JTA application-managed |
| public EntityManager | Returns: entity manager instancehow and when the entity manager should
be synchronized with the current JTA
transaction synchronizationType, Map<?, ?> properties for entity manager map)Create a new JTA application-managed |
| public Cache | |
| public CriteriaBuilder | Returns: an instance ofCriteriaBuilderReturn an instance of |
| public Metamodel | Returns: an instance ofMetamodelReturn an instance of the |
| public String | |
| public < the specified upper bound on the entity graph types E> Map | Returns: a map keyed by graph nameany Java type, including entityType)Object.class
meaning all entity graphsA map keyed by graph name, containing every named entity graph whose entity type is assignable to the given Java type. |
| public < the specified upper bound on the query result types R> Map | Returns: a map keyed by query nameany Java type, including resultType)Object.class
meaning all queriesA map keyed by query name, containing references to every named query whose result type is assignable to the given Java type. |
| public PersistenceUnitUtil | Returns: an instance ofPersistenceUnitUtilReturn interface providing access to utility methods for the persistence unit. |
| public Map | Returns: propertiesGet the properties and associated values that are in effect for the entity manager factory. |
| public SchemaManager | Returns: an instance ofSchemaManagerReturn interface providing access to schema management operations for the persistence unit. |
| public PersistenceUnitTransactionType | getTransactionType()
The type of transaction management used by this persistence unit, either resource-local transaction management, or JTA. |
| public boolean | Returns: boolean indicating whether the factory is openIndicates whether the factory is open. |
| public void | runInTransaction(Consumer<EntityManager>
a function to be executed in the scope of the transaction work)Create a new application-managed |
| public <T> T |
| addNamedEntityGraph | back to summary |
|---|---|
| public <T> void addNamedEntityGraph(String graphName, EntityGraph<T> entityGraph) Add a named copy of the given
| |
| addNamedQuery | back to summary |
|---|---|
| public void addNamedQuery(String name, Query query) Define the query, typed query, or stored procedure query as
a named query such that future query objects can be created
from it using the Any configuration of the query object (except for actual parameter binding) in effect when the named query is added is retained as part of the named query definition. This includes configuration information such as max results, hints, flush mode, lock mode, result set mapping information, and information about stored procedure parameters. When the query is executed, information that can be set by
means of the query APIs can be overridden. Information that is
overridden does not affect the named query as registered with
the entity manager factory, and thus does not affect subsequent
query objects created from it by calling If a named query of the same name has been previously defined, either statically via metadata or via this method, that query definition is replaced.
| |
| callInTransaction | back to summary |
|---|---|
| public <R> R callInTransaction(Function<EntityManager, R> work) Create a new application-managed
If the transaction type of the persistence unit is JTA, and there is a JTA
transaction already associated with the caller, then the
Otherwise, if the transaction type of the persistence unit is resource-local,
or if there is no JTA transaction already associated with the caller, then
the
Finally, the
| |
| close | back to summary |
|---|---|
| public void close() Redeclares java. Close the factory, releasing any resources that it holds.
After a factory instance has been closed, all methods invoked
on it will throw the
| |
| createEntityManager | back to summary |
|---|---|
| public EntityManager createEntityManager() Create a new application-managed The
| |
| createEntityManager | back to summary |
|---|---|
| public EntityManager createEntityManager(Map<?, ?> map) Create a new application-managed The
| |
| createEntityManager | back to summary |
|---|---|
| public EntityManager createEntityManager(SynchronizationType synchronizationType) Create a new JTA application-managed The
| |
| createEntityManager | back to summary |
|---|---|
| public EntityManager createEntityManager(SynchronizationType synchronizationType, Map<?, ?> map) Create a new JTA application-managed The
| |
| getCache | back to summary |
|---|---|
| public Cache getCache() Access the cache that is associated with the entity manager factory (the "second level cache").
| |
| getCriteriaBuilder | back to summary |
|---|---|
| public CriteriaBuilder getCriteriaBuilder() Return an instance of
| |
| getMetamodel | back to summary |
|---|---|
| public Metamodel getMetamodel() Return an instance of the
| |
| getName | back to summary |
|---|---|
| public String getName() The name of the persistence unit.
| |
| getNamedEntityGraphs | back to summary |
|---|---|
| public <E> Map A map keyed by graph name, containing every named entity graph whose entity type is assignable to the given Java type.
| |
| getNamedQueries | back to summary |
|---|---|
| public <R> Map A map keyed by query name, containing references to every named query whose result type is assignable to the given Java type.
| |
| getPersistenceUnitUtil | back to summary |
|---|---|
| public PersistenceUnitUtil getPersistenceUnitUtil() Return interface providing access to utility methods for the persistence unit.
| |
| getProperties | back to summary |
|---|---|
| public Map Get the properties and associated values that are in effect for the entity manager factory. Changing the contents of the map does not change the configuration in effect.
| |
| getSchemaManager | back to summary |
|---|---|
| public SchemaManager getSchemaManager() Return interface providing access to schema management operations for the persistence unit.
| |
| getTransactionType | back to summary |
|---|---|
| public PersistenceUnitTransactionType getTransactionType() The type of transaction management used by this persistence unit, either resource-local transaction management, or JTA.
| |
| isOpen | back to summary |
|---|---|
| public boolean isOpen() Indicates whether the factory is open. Returns true until the factory has been closed.
| |
| runInTransaction | back to summary |
|---|---|
| public void runInTransaction(Consumer<EntityManager> work) Create a new application-managed
If the transaction type of the persistence unit is JTA, and there is a JTA
transaction already associated with the caller, then the
Otherwise, if the transaction type of the persistence unit is resource-local,
or if there is no JTA transaction already associated with the caller, then
the
Finally, the
| |
| unwrap | back to summary |
|---|---|
| public <T> T unwrap(Class<T> cls) Return an object of the specified type to allow access to
a provider-specific API. If the provider implementation of
| |