Interface EntityAgent
- All Superinterfaces:
AutoCloseable, EntityHandler
EntityManager.
An instance of EntityAgent must be obtained from
an EntityManagerFactory and is only able to manage
persistence of entities belonging to the associated persistence
unit. In the Jakarta EE environment, an entity agent with a
lifecycle managed by the container may be obtained by
dependency injection.
- An
EntityAgentobtained directly from anEntityManagerFactoryis never thread safe, and it is always wrong to share a reference to such an entity agent between multiple concurrently executing threads. - On the other hand, in the Jakarta EE container environment,
a reference to a container-managed, transaction-scoped
entity agent obtained by injection is safe to invoke
concurrently from distinct threads. The container
redirects invocations to distinct instances of
EntityAgentbased on transaction affinity.
An EntityAgent has no associated persistence context,
and works only with detached entity instances. When a method of
this interface is called, any necessary interaction with the
database happens immediately and synchronously. In particular,
update is an explicit operation. Since
there is no flush operation,
and since the entities themselves are detached, modifications to
the entities are never automatically detected and made persistent.
- Since:
- 4.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidDelete a record.voiddeleteMultiple(List<?> entities) Delete every record in the given list.<T> Tfetch(T association) Fetch an association or collection that was configured for lazy loading if it has not yet been fetched from the database.voidInsert a record.voidinsertMultiple(List<?> entities) Insert every record in the given list.voidRefresh the entity instance state from the database.voidrefresh(Object entity, LockModeType lockMode) Refresh the entity instance state from the database.voidrefreshMultiple(List<?> entities) Refresh multiple entities.voidUpdate a record.voidupdateMultiple(List<?> entities) Update every record in the given list.voidPerform an upsert, that is, insert the record if it does not exist, or update it if it already exists.voidupsertMultiple(List<?> entities) Upsert every record in the given list.Methods inherited from interface EntityHandler
callWithConnection, close, createEntityGraph, createNamedQuery, createNamedQuery, createNamedStatement, createNamedStoredProcedureQuery, createNativeQuery, createNativeQuery, createNativeQuery, createNativeQuery, createNativeStatement, createQuery, createQuery, createQuery, createQuery, createQuery, createQuery, createStatement, createStatement, createStatement, createStoredProcedureQuery, createStoredProcedureQuery, createStoredProcedureQuery, find, find, find, findMultiple, findMultiple, get, get, get, getCacheRetrieveMode, getCacheStoreMode, getCriteriaBuilder, getEntityGraph, getEntityGraph, getEntityGraphs, getEntityManagerFactory, getMetamodel, getMultiple, getMultiple, getProperties, getTransaction, isOpen, runWithConnection, setCacheRetrieveMode, setCacheStoreMode, setProperty, unwrap
-
Method Details
-
insert
Insert a record.If the entity
Idfield is declared to be generated, for example, if it is annotatedGeneratedValue, the id is generated and assigned to the given instance.- The
PreInsertcallback is triggered. - The
PostInsertcallback is triggered if the operation is successful.
- Parameters:
entity- a new or removed entity instance- Throws:
IllegalArgumentException- if the given instance is determined to not be new or removedEntityExistsException- if the given entity has an identifier assigned by the application, and a record with the assigned identifier already exists in the databasePersistenceException- if a record could not be inserted in the database
- The
-
insertMultiple
Insert every record in the given list. The records are inserted in the order in which they occur in the given list and according to the requirements of theinsert(Object)method.- Parameters:
entities- The entities to be inserted.- Throws:
IllegalArgumentException- if one of the given instances is determined to not be new or removedEntityExistsException- if one of the given entities has an identifier assigned by the application, and a record with the assigned identifier already exists in the databasePersistenceException- if a record could not be inserted in the database- See Also:
-
update
Update a record.- The
PreUpdatecallback is triggered. - The
PostUpdatecallback is triggered if the operation is successful.
- Parameters:
entity- a detached entity instance- Throws:
IllegalArgumentException- if the given instance is determined to not be detachedOptimisticLockException- if an optimistic locking conflict is detected, that is, if no row matching the identifier of the given entity exists in the database or if an optimistic version check failsPersistenceException- if a record could not be updated in the database
- The
-
updateMultiple
Update every record in the given list. The records are updated in the order in which they occur in the given list and according to the requirements of theupdate(Object)method.- Parameters:
entities- The entities to be updated.- Throws:
IllegalArgumentException- if the one of the given instances is determined to not be detachedOptimisticLockException- if an optimistic locking conflict is detected, that is, if no row matching the identifier of one of the given entities exists in the database or if an optimistic version check failsPersistenceException- if a record could not be updated in the database- See Also:
-
delete
Delete a record.- The
PreDeletecallback is triggered. - The
PostDeletecallback is triggered if the operation is successful.
- Parameters:
entity- a detached entity instance- Throws:
IllegalArgumentException- if the given instance is determined to not be detachedOptimisticLockException- if an optimistic locking conflict is detected, that is, if no row matching the identifier of the given entity exists in the database or if an optimistic version check failsPersistenceException- if a record could not be deleted from the database
- The
-
deleteMultiple
Delete every record in the given list. The records are deleted in the order in which they occur in the given list and according to the requirements of thedelete(Object)method.- Parameters:
entities- The entities to be deleted.- Throws:
IllegalArgumentException- if the one of the given instances is determined to not be detachedOptimisticLockException- if an optimistic locking conflict is detected, that is, if no row matching the identifier of one of the given entities exists in the database or if an optimistic version check failsPersistenceException- if a record could not be deleted from the database- See Also:
-
upsert
Perform an upsert, that is, insert the record if it does not exist, or update it if it already exists.This method never performs id generation and does not accept an entity instance with a null identifier. When id generation is required, use
insert(Object).On the other hand,
upsert()does accept an entity instance with an assigned identifier value, even if the entityIdfield is declared to be generated, for example, if it is annotatedGeneratedValue. Thus, this method may be used to import data from an external source.- The
PreUpsertcallback is triggered. - The
PostUpsertcallback is triggered if the operation is successful.
- Parameters:
entity- a detached entity instance, or a new instance with an assigned identifier- Throws:
IllegalArgumentException- if the given entity has a null identifier valueOptimisticLockException- if an optimistic locking conflict is detected, that is, if an optimistic version check failsPersistenceException- if a record could not be upserted in the database
- The
-
upsertMultiple
Upsert every record in the given list. The records are upserted in the order in which they occur in the given list and according to the requirements of theupsert(Object)method.- Parameters:
entities- The entities to be inserted or updated.- Throws:
IllegalArgumentException- if one of the given entities has a null identifier valueOptimisticLockException- if an optimistic locking conflict is detected, that is, if an optimistic version check failsPersistenceException- if a record could not be upserted in the database- See Also:
-
refresh
Refresh the entity instance state from the database.- Parameters:
entity- The entity to be refreshed.- Throws:
EntityNotFoundException- if the given entity no longer exists in the databasePersistenceException- if a record could not be read from the database
-
refreshMultiple
Refresh multiple entities.- Parameters:
entities- The entities to be refreshed.- Throws:
EntityNotFoundException- if one of the given entities no longer exists in the databasePersistenceException- if a record could not be read from the database- See Also:
-
refresh
Refresh the entity instance state from the database.- Parameters:
entity- The entity to be refreshed.lockMode- The LockMode to be applied.- Throws:
EntityNotFoundException- if the given entity no longer exists in the databasePersistenceException- if a record could not be read from the database
-
fetch
<T> T fetch(T association) Fetch an association or collection that was configured for lazy loading if it has not yet been fetched from the database. If the association or collection is already fetched, this operation has no effect. For convenience, this method always returns its argument.Book book = agent.get(Book.class, isbn); // book is immediately detached agent.fetch(book.getAuthors()) // fetch the associated authors .forEach(author -> ... ); // iterate the collection- Type Parameters:
T- The type of the entity attribute type, either an entity type or a collection type- Parameters:
association- A lazy association- Returns:
- The association with its state fetched.
- Throws:
PersistenceException- if a record could not be read from the database
-