public class JPABaseDAO
extends java.lang.Object
Base class for DAOs that uses JPA EnityManagers and JPA Query Language.
The SearchProcessor
and EntityManager
must be set
in order for the DAO to function. Generally, a single
SearchProcessor will be associated with an instance of a DAO for
the lifetime of the instance, while a new "current" EntityManager will be
injected as needed. Make sure that any EntityManager that is used is
associated with the same persistence unit (i.e. EntityManagerFactory) as the
SearchProcessor.
Constructor and Description |
---|
JPABaseDAO() |
Modifier and Type | Method and Description |
---|---|
protected <T> java.util.List<T> |
_all(java.lang.Class<T> type)
Get a list of all the entities of the specified class.
|
protected boolean |
_contains(java.lang.Object o)
Returns true if the object is connected to the current hibernate session.
|
protected int |
_count(java.lang.Class<?> type)
Returns the number of instances of this entity in the datastore.
|
protected int |
_count(java.lang.Class<?> searchClass,
ISearch search)
Same as
_count(ISearch) except that it uses the specified
search class instead of getting it from the search object. |
protected int |
_count(ISearch search)
Returns the total number of results that would be returned using the
given
ISearch if there were no paging or maxResult limits. |
protected boolean[] |
_exists(java.lang.Class<?> type,
java.io.Serializable... ids) |
protected boolean |
_exists(java.lang.Class<?> type,
java.io.Serializable id) |
protected boolean |
_exists(java.lang.Object entity) |
protected <T> T[] |
_find(java.lang.Class<T> type,
java.io.Serializable... ids)
Return the all the persistent instances of the given entity class with
the given identifiers.
|
protected <T> T |
_find(java.lang.Class<T> type,
java.io.Serializable id)
Return the persistent instance of the given entity class with the given
identifier, or null if there is no such persistent instance.
|
protected void |
_flush()
Flushes changes in the hibernate cache to the datastore.
|
protected Filter |
_getFilterFromExample(java.lang.Object example) |
protected Filter |
_getFilterFromExample(java.lang.Object example,
ExampleOptions options) |
protected <T> T |
_getReference(java.lang.Class<T> type,
java.io.Serializable id) |
protected <T> T[] |
_getReferences(java.lang.Class<T> type,
java.io.Serializable... ids) |
protected <T> T[] |
_merge(java.lang.Class<T> arrayType,
T... entities)
Copy the state of the given objects onto the persistent objects with the
same identifier.
|
protected <T> T |
_merge(T entity)
Copy the state of the given object onto the persistent object with the
same identifier.
|
protected void |
_persist(java.lang.Object... entities)
Make a transient instance persistent and add it to the datastore.
|
protected <T> T[] |
_persistOrMerge(java.lang.Class<T> arrayType,
T... entities)
For each entity: If an entity with the same ID already exists in the
database, merge the changes into that entity.
|
protected <T> T |
_persistOrMerge(T entity)
If an entity with the same ID already exists in the database, merge the
changes into that entity.
|
protected void |
_refresh(java.lang.Object... entities)
Refresh the content of the given entity from the current datastore state.
|
protected boolean |
_removeById(java.lang.Class<?> type,
java.io.Serializable id)
Remove the entity of the specified class with the specified id from the
datastore.
|
protected void |
_removeByIds(java.lang.Class<?> type,
java.io.Serializable... ids)
Remove all the entities of the given type from the datastore that have
one of these ids.
|
protected void |
_removeEntities(java.lang.Object... entities)
Remove the specified entities from the datastore.
|
protected boolean |
_removeEntity(java.lang.Object entity)
Remove the specified entity from the datastore.
|
protected java.util.List |
_search(java.lang.Class<?> searchClass,
ISearch search)
Same as
_search(ISearch) except that it uses the specified
search class instead of getting it from the search object. |
protected java.util.List |
_search(ISearch search)
Search for objects based on the search parameters in the specified
ISearch object. |
protected SearchResult |
_searchAndCount(java.lang.Class<?> searchClass,
ISearch search)
Same as
_searchAndCount(ISearch) except that it uses the
specified search class instead of getting it from the search object. |
protected SearchResult |
_searchAndCount(ISearch search)
Returns a
SearchResult object that includes the list of
results like search() and the total length like
searchLength . |
protected java.lang.Object |
_searchUnique(java.lang.Class<?> searchClass,
ISearch search)
Same as
_searchUnique(ISearch) except that it uses the
specified search class instead of getting it from the search object. |
protected java.lang.Object |
_searchUnique(ISearch search)
Search for a single result using the given parameters.
|
protected javax.persistence.EntityManager |
em()
Get the current EntityManager
|
protected MetadataUtil |
getMetadataUtil() |
protected JPASearchProcessor |
getSearchProcessor() |
void |
setEntityManager(javax.persistence.EntityManager entityManager)
Set the current EntityManager
|
void |
setSearchProcessor(JPASearchProcessor searchProcessor) |
public void setSearchProcessor(JPASearchProcessor searchProcessor)
protected JPASearchProcessor getSearchProcessor()
public void setEntityManager(javax.persistence.EntityManager entityManager)
entityManager
- protected javax.persistence.EntityManager em()
protected MetadataUtil getMetadataUtil()
protected void _persist(java.lang.Object... entities)
Make a transient instance persistent and add it to the datastore. This operation cascades to associated instances if the association is mapped with cascade="persist". Throws an error if the entity already exists.
Does not guarantee that the object will be assigned an identifier
immediately. With persist
a datastore-generated id may not
be pulled until flush time.
protected boolean _removeById(java.lang.Class<?> type, java.io.Serializable id)
true
if the object is found in the datastore and
deleted, false
if the item is not found.protected void _removeByIds(java.lang.Class<?> type, java.io.Serializable... ids)
protected boolean _removeEntity(java.lang.Object entity)
true
if the object is found in the datastore and
removed, false
if the item is not found.protected void _removeEntities(java.lang.Object... entities)
protected <T> T _find(java.lang.Class<T> type, java.io.Serializable id)
protected <T> T[] _find(java.lang.Class<T> type, java.io.Serializable... ids)
protected <T> T _getReference(java.lang.Class<T> type, java.io.Serializable id)
protected <T> T[] _getReferences(java.lang.Class<T> type, java.io.Serializable... ids)
protected <T> java.util.List<T> _all(java.lang.Class<T> type)
protected <T> T _merge(T entity)
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy and return it as a newly persistent instance.
The instance that is passed in does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".
protected <T> T[] _merge(java.lang.Class<T> arrayType, T... entities)
Copy the state of the given objects onto the persistent objects with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instances. If a given instance is unsaved, save a copy and return it as a newly persistent instance.
The instances that are passed in do not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".
protected <T> T _persistOrMerge(T entity)
protected <T> T[] _persistOrMerge(java.lang.Class<T> arrayType, T... entities)
For each entity: If an entity with the same ID already exists in the database, merge the changes into that entity. If not persist the given entity. In either case, a managed entity with the changed values is returned. It may or may not be the same object as was passed in.
This version of the method allows the array type to be specified.
protected java.util.List _search(ISearch search)
ISearch
object.ISearch
protected java.util.List _search(java.lang.Class<?> searchClass, ISearch search)
_search(ISearch)
except that it uses the specified
search class instead of getting it from the search object. Also, if the
search object has a different search class than what is specified, an
exception is thrown.protected int _count(ISearch search)
ISearch
if there were no paging or maxResult limits.ISearch
protected int _count(java.lang.Class<?> searchClass, ISearch search)
_count(ISearch)
except that it uses the specified
search class instead of getting it from the search object. Also, if the
search object has a different search class than what is specified, an
exception is thrown.protected int _count(java.lang.Class<?> type)
protected SearchResult _searchAndCount(ISearch search)
SearchResult
object that includes the list of
results like search()
and the total length like
searchLength
.ISearch
protected SearchResult _searchAndCount(java.lang.Class<?> searchClass, ISearch search)
_searchAndCount(ISearch)
except that it uses the
specified search class instead of getting it from the search object.
Also, if the search object has a different search class than what is
specified, an exception is thrown.protected java.lang.Object _searchUnique(ISearch search) throws javax.persistence.NonUniqueResultException, javax.persistence.NoResultException
javax.persistence.NoResultException
- if there is no resultjavax.persistence.NonUniqueResultException
- if more than one resultprotected java.lang.Object _searchUnique(java.lang.Class<?> searchClass, ISearch search) throws javax.persistence.NonUniqueResultException, javax.persistence.NoResultException
_searchUnique(ISearch)
except that it uses the
specified search class instead of getting it from the search object.
Also, if the search object has a different search class than what is
specified, an exception is thrown.javax.persistence.NoResultException
- if there is no resultjavax.persistence.NonUniqueResultException
- if more than one resultprotected boolean _contains(java.lang.Object o)
protected void _flush()
protected void _refresh(java.lang.Object... entities)
protected boolean _exists(java.lang.Object entity)
protected boolean _exists(java.lang.Class<?> type, java.io.Serializable id)
protected boolean[] _exists(java.lang.Class<?> type, java.io.Serializable... ids)
protected Filter _getFilterFromExample(java.lang.Object example)
protected Filter _getFilterFromExample(java.lang.Object example, ExampleOptions options)