com.googlecode.genericdao.dao.jpa
Class GenericDAOImpl<T,ID extends Serializable>

java.lang.Object
  extended by com.googlecode.genericdao.dao.jpa.JPABaseDAO
      extended by com.googlecode.genericdao.dao.jpa.GenericDAOImpl<T,ID>
Type Parameters:
T - The type of the domain object for which this instance is to be used.
ID - The type of the id of the domain object for which this instance is to be used.
All Implemented Interfaces:
GenericDAO<T,ID>

public class GenericDAOImpl<T,ID extends Serializable>
extends JPABaseDAO
implements GenericDAO<T,ID>

Implementation of GenericDAO using Hibernate. The SessionFactory property is annotated for automatic resource injection.

Author:
dwolverton

Field Summary
protected  Class<T> persistentClass
           
 
Constructor Summary
GenericDAOImpl()
           
 
Method Summary
 int count(ISearch search)
          Returns the total number of results that would be returned using the given ISearch if there were no paging or maxResults limits.
 T[] find(ID... ids)
          Get all entities of the specified type from the datastore that have one of these ids.
 T find(ID id)
           Get the entity with the specified type and id from the datastore.
 List<T> findAll()
          Get a list of all the objects of the specified type.
 void flush()
          Flushes changes in the Hibernate session to the datastore.
 Filter getFilterFromExample(T example)
          Generates a search filter from the given example using default options.
 Filter getFilterFromExample(T example, ExampleOptions options)
          Generates a search filter from the given example using the specified options.
 T getReference(ID id)
           Get a reference to the entity with the specified type and id from the datastore.
 T[] getReferences(ID... ids)
           Get a reference to the entities of the specified type with the given ids from the datastore.
 boolean isAttached(T entity)
          Returns true if the object is connected to the current Hibernate session.
 T[] merge(T... entities)
           Copy the state of the given objects onto the persistent objects with the same identifier.
 T merge(T entity)
           Copy the state of the given object onto the persistent object with the same identifier.
 void persist(T... entities)
           Make a transient instance persistent and add it to the datastore.
 void refresh(T... entities)
          Refresh the content of the given entity from the current datastore state.
 void remove(T... entities)
          Remove all of the specified entities from the datastore.
 boolean remove(T entity)
          Remove the specified entity from the datastore.
 boolean removeById(ID id)
          Remove the entity with the specified type and id from the datastore.
 void removeByIds(ID... ids)
          Remove all the entities of the given type from the datastore that have one of these ids.
 T[] save(T... entities)
           For each entity: If an entity with the same ID already exists in the database, merge the changes into that entity.
 T save(T entity)
          If an entity with the same ID already exists in the database, merge the changes into that entity.
<RT> List<RT>
search(ISearch search)
          Search for entities given the search parameters in the specified ISearch object.
<RT> SearchResult<RT>
searchAndCount(ISearch search)
          Returns a SearchResult object that includes both the list of results like search() and the total length like count().
<RT> RT
searchUnique(ISearch search)
          Search for a single entity using the given parameters.
 
Methods inherited from class com.googlecode.genericdao.dao.jpa.JPABaseDAO
_all, _contains, _count, _count, _count, _exists, _exists, _exists, _find, _find, _flush, _getFilterFromExample, _getFilterFromExample, _getReference, _getReferences, _merge, _merge, _persist, _persistOrMerge, _persistOrMerge, _refresh, _removeById, _removeByIds, _removeEntities, _removeEntity, _search, _search, _searchAndCount, _searchAndCount, _searchUnique, _searchUnique, em, getMetadataUtil, getSearchProcessor, setEntityManager, setSearchProcessor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

persistentClass

protected Class<T> persistentClass
Constructor Detail

GenericDAOImpl

public GenericDAOImpl()
Method Detail

count

public int count(ISearch search)
Description copied from interface: GenericDAO
Returns the total number of results that would be returned using the given ISearch if there were no paging or maxResults limits.

Specified by:
count in interface GenericDAO<T,ID extends Serializable>

find

public T find(ID id)
Description copied from interface: GenericDAO

Get the entity with the specified type and id from the datastore.

If none is found, return null.

Specified by:
find in interface GenericDAO<T,ID extends Serializable>

find

public T[] find(ID... ids)
Description copied from interface: GenericDAO
Get all entities of the specified type from the datastore that have one of these ids.

Specified by:
find in interface GenericDAO<T,ID extends Serializable>

findAll

public List<T> findAll()
Description copied from interface: GenericDAO
Get a list of all the objects of the specified type.

Specified by:
findAll in interface GenericDAO<T,ID extends Serializable>

flush

public void flush()
Description copied from interface: GenericDAO
Flushes changes in the Hibernate session to the datastore.

Specified by:
flush in interface GenericDAO<T,ID extends Serializable>

getReference

public T getReference(ID id)
Description copied from interface: GenericDAO

Get a reference to the entity with the specified type and id from the datastore.

This does not require a call to the datastore and does not populate any of the entity's values. Values may be fetched lazily at a later time. This increases performance if a another entity is being saved that should reference this entity but the values of this entity are not needed.

Specified by:
getReference in interface GenericDAO<T,ID extends Serializable>

getReferences

public T[] getReferences(ID... ids)
Description copied from interface: GenericDAO

Get a reference to the entities of the specified type with the given ids from the datastore.

This does not require a call to the datastore and does not populate any of the entities' values. Values may be fetched lazily at a later time. This increases performance if a another entity is being saved that should reference these entities but the values of these entities are not needed.

Specified by:
getReferences in interface GenericDAO<T,ID extends Serializable>

isAttached

public boolean isAttached(T entity)
Description copied from interface: GenericDAO
Returns true if the object is connected to the current Hibernate session.

Specified by:
isAttached in interface GenericDAO<T,ID extends Serializable>

refresh

public void refresh(T... entities)
Description copied from interface: GenericDAO
Refresh the content of the given entity from the current datastore state.

Specified by:
refresh in interface GenericDAO<T,ID extends Serializable>

remove

public boolean remove(T entity)
Description copied from interface: GenericDAO
Remove the specified entity from the datastore.

Specified by:
remove in interface GenericDAO<T,ID extends Serializable>
Returns:
true if the entity is found in the datastore and removed, false if it is not found.

remove

public void remove(T... entities)
Description copied from interface: GenericDAO
Remove all of the specified entities from the datastore.

Specified by:
remove in interface GenericDAO<T,ID extends Serializable>

removeById

public boolean removeById(ID id)
Description copied from interface: GenericDAO
Remove the entity with the specified type and id from the datastore.

Specified by:
removeById in interface GenericDAO<T,ID extends Serializable>
Returns:
true if the entity is found in the datastore and removed, false if it is not found.

removeByIds

public void removeByIds(ID... ids)
Description copied from interface: GenericDAO
Remove all the entities of the given type from the datastore that have one of these ids.

Specified by:
removeByIds in interface GenericDAO<T,ID extends Serializable>

merge

public T merge(T entity)
Description copied from interface: GenericDAO

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

Specified by:
merge in interface GenericDAO<T,ID extends Serializable>

merge

public T[] merge(T... entities)
Description copied from interface: GenericDAO

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

Specified by:
merge in interface GenericDAO<T,ID extends Serializable>

persist

public void persist(T... entities)
Description copied from interface: GenericDAO

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.

Specified by:
persist in interface GenericDAO<T,ID extends Serializable>

save

public T save(T entity)
Description copied from interface: GenericDAO
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.

Specified by:
save in interface GenericDAO<T,ID extends Serializable>

save

public T[] save(T... entities)
Description copied from interface: GenericDAO

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.

Specified by:
save in interface GenericDAO<T,ID extends Serializable>
Returns:
an array containing each managed entity corresponding to the entities passed in.

search

public <RT> List<RT> search(ISearch search)
Description copied from interface: GenericDAO
Search for entities given the search parameters in the specified ISearch object.

Specified by:
search in interface GenericDAO<T,ID extends Serializable>

searchAndCount

public <RT> SearchResult<RT> searchAndCount(ISearch search)
Description copied from interface: GenericDAO
Returns a SearchResult object that includes both the list of results like search() and the total length like count().

Specified by:
searchAndCount in interface GenericDAO<T,ID extends Serializable>

searchUnique

public <RT> RT searchUnique(ISearch search)
Description copied from interface: GenericDAO
Search for a single entity using the given parameters.

Specified by:
searchUnique in interface GenericDAO<T,ID extends Serializable>

getFilterFromExample

public Filter getFilterFromExample(T example)
Description copied from interface: GenericDAO
Generates a search filter from the given example using default options.

Specified by:
getFilterFromExample in interface GenericDAO<T,ID extends Serializable>

getFilterFromExample

public Filter getFilterFromExample(T example,
                                   ExampleOptions options)
Description copied from interface: GenericDAO
Generates a search filter from the given example using the specified options.

Specified by:
getFilterFromExample in interface GenericDAO<T,ID extends Serializable>


Copyright © 2008-2011. All Rights Reserved.