com.googlecode.genericdao.dao.jpa
Class JPABaseDAO

java.lang.Object
  extended by com.googlecode.genericdao.dao.jpa.JPABaseDAO
Direct Known Subclasses:
GeneralDAOImpl, GenericDAOImpl

public class JPABaseDAO
extends 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.

Author:
dwolverton

Constructor Summary
JPABaseDAO()
           
 
Method Summary
protected
<T> List<T>
_all(Class<T> type)
          Get a list of all the entities of the specified class.
protected  boolean _contains(Object o)
          Returns true if the object is connected to the current hibernate session.
protected  int _count(Class<?> type)
          Returns the number of instances of this entity in the datastore.
protected  int _count(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(Class<?> type, Serializable... ids)
           
protected  boolean _exists(Class<?> type, Serializable id)
           
protected  boolean _exists(Object entity)
           
protected
<T> T[]
_find(Class<T> type, Serializable... ids)
          Return the all the persistent instances of the given entity class with the given identifiers.
protected
<T> T
_find(Class<T> type, 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(Object example)
           
protected  Filter _getFilterFromExample(Object example, ExampleOptions options)
           
protected
<T> T
_getReference(Class<T> type, Serializable id)
           
protected
<T> T[]
_getReferences(Class<T> type, Serializable... ids)
           
protected
<T> T[]
_merge(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(Object... entities)
           Make a transient instance persistent and add it to the datastore.
protected
<T> T[]
_persistOrMerge(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(Object... entities)
          Refresh the content of the given entity from the current datastore state.
protected  boolean _removeById(Class<?> type, Serializable id)
          Remove the entity of the specified class with the specified id from the datastore.
protected  void _removeByIds(Class<?> type, Serializable... ids)
          Remove all the entities of the given type from the datastore that have one of these ids.
protected  void _removeEntities(Object... entities)
          Remove the specified entities from the datastore.
protected  boolean _removeEntity(Object entity)
          Remove the specified entity from the datastore.
protected  List _search(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  List _search(ISearch search)
          Search for objects based on the search parameters in the specified ISearch object.
protected  SearchResult _searchAndCount(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  Object _searchUnique(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  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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JPABaseDAO

public JPABaseDAO()
Method Detail

setSearchProcessor

public void setSearchProcessor(JPASearchProcessor searchProcessor)

getSearchProcessor

protected JPASearchProcessor getSearchProcessor()

setEntityManager

public void setEntityManager(javax.persistence.EntityManager entityManager)
Set the current EntityManager

Parameters:
entityManager -

em

protected javax.persistence.EntityManager em()
Get the current EntityManager


getMetadataUtil

protected MetadataUtil getMetadataUtil()

_persist

protected void _persist(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.


_removeById

protected boolean _removeById(Class<?> type,
                              Serializable id)
Remove the entity of the specified class with the specified id from the datastore.

Returns:
true if the object is found in the datastore and deleted, false if the item is not found.

_removeByIds

protected void _removeByIds(Class<?> type,
                            Serializable... ids)
Remove all the entities of the given type from the datastore that have one of these ids.


_removeEntity

protected boolean _removeEntity(Object entity)
Remove the specified entity from the datastore.

Returns:
true if the object is found in the datastore and removed, false if the item is not found.

_removeEntities

protected void _removeEntities(Object... entities)
Remove the specified entities from the datastore.


_find

protected <T> T _find(Class<T> type,
                      Serializable id)
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.


_find

protected <T> T[] _find(Class<T> type,
                        Serializable... ids)
Return the all the persistent instances of the given entity class with the given identifiers. An array of entities is returned that matches the same order of the ids listed in the call. For each entity that is not found in the datastore, a null will be inserted in its place in the return array.


_getReference

protected <T> T _getReference(Class<T> type,
                              Serializable id)

_getReferences

protected <T> T[] _getReferences(Class<T> type,
                                 Serializable... ids)

_all

protected <T> List<T> _all(Class<T> type)
Get a list of all the entities of the specified class.


_merge

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".


_merge

protected <T> T[] _merge(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".


_persistOrMerge

protected <T> T _persistOrMerge(T 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.


_persistOrMerge

protected <T> T[] _persistOrMerge(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.

Returns:
an array containing each managed entity corresponding to the entities passed in.

_search

protected List _search(ISearch search)
Search for objects based on the search parameters in the specified ISearch object.

See Also:
ISearch

_search

protected List _search(Class<?> searchClass,
                       ISearch search)
Same as _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.


_count

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.

See Also:
ISearch

_count

protected int _count(Class<?> searchClass,
                     ISearch search)
Same as _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.


_count

protected int _count(Class<?> type)
Returns the number of instances of this entity in the datastore.


_searchAndCount

protected SearchResult _searchAndCount(ISearch search)
Returns a SearchResult object that includes the list of results like search() and the total length like searchLength.

See Also:
ISearch

_searchAndCount

protected SearchResult _searchAndCount(Class<?> searchClass,
                                       ISearch search)
Same as _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.


_searchUnique

protected Object _searchUnique(ISearch search)
                        throws javax.persistence.NonUniqueResultException,
                               javax.persistence.NoResultException
Search for a single result using the given parameters.

Throws:
javax.persistence.NoResultException - if there is no result
javax.persistence.NonUniqueResultException - if more than one result

_searchUnique

protected Object _searchUnique(Class<?> searchClass,
                               ISearch search)
                        throws javax.persistence.NonUniqueResultException,
                               javax.persistence.NoResultException
Same as _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.

Throws:
javax.persistence.NoResultException - if there is no result
javax.persistence.NonUniqueResultException - if more than one result

_contains

protected boolean _contains(Object o)
Returns true if the object is connected to the current hibernate session.


_flush

protected void _flush()
Flushes changes in the hibernate cache to the datastore.


_refresh

protected void _refresh(Object... entities)
Refresh the content of the given entity from the current datastore state.


_exists

protected boolean _exists(Object entity)

_exists

protected boolean _exists(Class<?> type,
                          Serializable id)

_exists

protected boolean[] _exists(Class<?> type,
                            Serializable... ids)

_getFilterFromExample

protected Filter _getFilterFromExample(Object example)

_getFilterFromExample

protected Filter _getFilterFromExample(Object example,
                                       ExampleOptions options)


Copyright © 2008-2011. All Rights Reserved.