@Status(stage=DEVELOPMENT, unitTests=PARTIAL) @Review(by="Kees Schotanus", at="2009-09-28") public final class EntityDataAccess extends Object
EntityManager
that
directly deal with one or more JPA Entities. Use this class in combination
with EntityManagerHelper
for example to handle transactions.Modifier and Type | Method and Description |
---|---|
static <T> boolean |
contains(T entity)
Determines whether the supplied entity belongs to the current persistence
context.
|
static <T> List<T> |
executeNamedQuery(javax.persistence.EntityManager entityManager,
String queryName,
Map<String,Object> parameterNamesToValues,
Class<T> type)
Executes a named query using the supplied entityManager.
|
static <T> List<T> |
executeNamedQuery(String queryName,
Map<String,Object> queryParameters,
Class<T> type)
Executes a named query.
|
static <T> List<T> |
findAll(Class<T> type)
Find all entities of the supplied class.
|
static <T> List<T> |
findAll(javax.persistence.EntityManager entityManager,
Class<T> type)
Find all entities of the supplied class, using the supplied
entityManager.
|
static <T> List<T> |
findByCriteria(Class<T> type,
Searchable searchCriteria)
Finds entities of type <T> by criteria.
|
static <T> List<T> |
findByCriteria(javax.persistence.EntityManager entityManager,
Class<T> type,
Searchable searchCriteria)
Finds entities of type <T> by criteria using the supplied entityManager.
|
static <T> T |
findById(Class<T> type,
Object primaryKey)
Finds a persisted entity by its id (primary key).
|
static javax.persistence.EntityManager |
getEntityManager()
Convenience method to get the EntityManagerHelper.
|
static <T> T |
getReference(Class<T> type,
Object primaryKey)
Gets a reference to a persisted entity by its id (primary key).
|
static void |
lock(Object entity,
javax.persistence.LockModeType lockMode)
Sets the supplied lockMode on the supplied entity.
|
static <T> T |
merge(T entity)
Merges the supplied entity with the persisted state of the supplied
entity.
|
static <T> T |
persist(T entity)
Persists the supplied entity that previously should not have been
persisted.
|
static void |
refresh(Object entity)
Refreshes the state of the supplied entity from the database.
|
static <T> void |
remove(Class<T> type,
Object primaryKey)
Removes a previously persisted entity.
|
public static javax.persistence.EntityManager getEntityManager()
public static <T> T persist(T entity)
merge(Object)
should be
used.T
- The type of the entity to persist.entity
- The entity to persists.NullPointerException
- When the supplied entity is null.javax.persistence.PersistenceException
- When the persist operation failed.EntityManager.persist(Object)
public static <T> T merge(T entity)
T
- The type of the entity to merge.entity
- The entity to merge.NullPointerException
- When the supplied entity is null.javax.persistence.PersistenceException
- when the merge operation failed.EntityManager.merge(Object)
public static <T> void remove(Class<T> type, Object primaryKey)
T
- The type of the entity to remove.type
- The class type of the entity to remove.primaryKey
- The primary key of the entity to remove.NullPointerException
- When the supplied primaryKey is null.javax.persistence.PersistenceException
- when the remove operation failed.EntityManager.remove(Object)
public static <T> boolean contains(T entity)
T
- The type of entity to check.entity
- The entity to check.NullPointerException
- When the supplied entity is null.EntityManager.contains(Object)
public static <T> T findById(Class<T> type, Object primaryKey)
T
- The type of the entity to find.type
- The class type of the entity to find.primaryKey
- The primary key of the entity to find.NullPointerException
- When either the supplied type or the
supplied primaryKey is null.EntityManager.find(Class, Object)
public static <T> List<T> findAll(Class<T> type)
T
- The type of the entities to find.type
- The class type of the entities to find.NullPointerException
- When the supplied type is null.public static <T> List<T> findAll(javax.persistence.EntityManager entityManager, Class<T> type)
T
- The type of the entities to find.entityManager
- The entity manager to use.type
- The class type of the entities to find.NullPointerException
- When either the supplied entityManager or
class is null.public static <T> List<T> findByCriteria(Class<T> type, Searchable searchCriteria)
T
- The type of the entities to find.type
- The class type of the entities to find.searchCriteria
- The search criteria that are used to find the
entities.NullPointerException
- When either the supplied theClass or
searchCriteria is null.public static <T> List<T> findByCriteria(javax.persistence.EntityManager entityManager, Class<T> type, Searchable searchCriteria)
T
- The type of the entities to find.entityManager
- The entity manager to use.type
- The class type of the entities to find.searchCriteria
- The search criteria that are used to find the
entities.NullPointerException
- When either the supplied entityManager or
theClass or searchCriteria is null.public static <T> List<T> executeNamedQuery(String queryName, Map<String,Object> queryParameters, Class<T> type)
T
- The type of the Persistable
to return.queryName
- Name of the query.queryParameters
- The query parameters.
type
- The class type of the entities to find.NullPointerException
- When either the supplied queryName or
theClass is null.IllegalArgumentException
- When the supplied queryName is empty.public static <T> List<T> executeNamedQuery(javax.persistence.EntityManager entityManager, String queryName, Map<String,Object> parameterNamesToValues, Class<T> type)
T
- The type of the Persistable
to return.entityManager
- The entity manager to use.queryName
- Name of the query.parameterNamesToValues
- The mapping of property names to property
values.
type
- The class type of the entities to find.NullPointerException
- When either the supplied queryName or
theClass is null.IllegalArgumentException
- When the supplied queryName is empty.public static <T> T getReference(Class<T> type, Object primaryKey)
EntityNotFoundException
upon first access.T
- The type of the entity to find.type
- The class type of the reference to get.primaryKey
- The primary key the entity to find.javax.persistence.EntityNotFoundException
- When the entity is not
found.NullPointerException
- When either the supplied type or the
supplied primaryKey is null.EntityManager.getReference(Class, Object)
public static void lock(Object entity, javax.persistence.LockModeType lockMode)
entity
- The entity to lock.lockMode
- The lock mode.IllegalArgumentException
- When the supplied instance is a detached
entity.IllegalStateException
- When the EntityManager is closed.NullPointerException
- When either the supplied entity or lockMode
is null.javax.persistence.PersistenceException
- When an unsupported lock
call is made.javax.persistence.TransactionRequiredException
- When no
transaction exists.public static void refresh(Object entity)
entity
- The entity to refresh.IllegalArgumentException
- When the supplied entity is not managed.IllegalStateException
- When the EntityManager is closed.NullPointerException
- When the supplied entity is null.javax.persistence.TransactionRequiredException
- When no
transaction exists.javax.persistence.EntityNotFoundException
- When the supplied
entity does no longer exists in the database.EntityManager.refresh(Object)
Copyright © 2008–2018. All rights reserved.