com.googlecode.genericdao.dao.jpa
Class DAODispatcher

java.lang.Object
  extended by com.googlecode.genericdao.dao.BaseDAODispatcher
      extended by com.googlecode.genericdao.dao.jpa.DAODispatcher
All Implemented Interfaces:
GeneralDAO

public class DAODispatcher
extends BaseDAODispatcher
implements GeneralDAO

This is an implementation of GeneralDAO that delegates to other DAOs depending on what entity class is being processed.

Set the specificDAOs Map in order to configure which DAO will be used for which entity class. If the map contains no entry for a given class, the generalDAO is used.

For example to dispatch operation on com.myproject.model.Customer to a DAO called customerDAO, set the map like this. (Of course tools like Spring can be used to do this configuration more elequently.)

 Map<String, Object> specificDAOs = new HashMap<String, Object>();
 specificDAOs.put("com.myproject.model.Customer", customerDAO);
 
 DAODispatcher dispatcher = new DAODispatcher();
 dispatcher.setSpecificDAOs(specificDAOs);
 

Author:
dwolverton

Field Summary
protected  GeneralDAO generalDAO
           
 
Fields inherited from class com.googlecode.genericdao.dao.BaseDAODispatcher
specificDAOs
 
Constructor Summary
DAODispatcher()
           
 
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> T[]
find(Class<T> type, Serializable... ids)
          Get all entities of the specified type from the datastore that have one of these ids.
<T> T
find(Class<T> type, Serializable id)
           Get the entity with the specified type and id from the datastore.
<T> List<T>
findAll(Class<T> type)
          Get a list of all the objects of the specified type.
 void flush()
          Deprecated. use flush(Class)
 void flush(Class<?> klass)
           
 Filter getFilterFromExample(Object example)
          Generates a search filter from the given example using default options.
 Filter getFilterFromExample(Object example, ExampleOptions options)
          Generates a search filter from the given example using the specified options.
<T> T
getReference(Class<T> type, Serializable id)
           Get a reference to the entity with the specified type and id from the datastore.
<T> T[]
getReferences(Class<T> type, Serializable... ids)
           Get a reference to the entities of the specified type with the given ids from the datastore.
 boolean isAttached(Object entity)
          Returns true if the object is connected to the current Hibernate session.
 Object[] merge(Object... entities)
           Copy the state of the given objects onto the persistent objects with the same identifier.
<T> T
merge(T entity)
           Copy the state of the given object onto the persistent object with the same identifier.
 void persist(Object... entities)
           Make a transient instance persistent and add it to the datastore.
 void refresh(Object... entities)
          Refresh the content of the given entity from the current datastore state.
 void remove(Object... entities)
          Remove all of the specified entities from the datastore.
 boolean remove(Object entity)
          Remove the specified entity from the datastore.
 boolean removeById(Class<?> type, Serializable id)
          Remove the entity with the specified type and id from the datastore.
 void removeByIds(Class<?> type, Serializable... ids)
          Remove all the entities of the given type from the datastore that have one of these ids.
 Object[] save(Object... entities)
           For each entity: If an entity with the same ID already exists in the database, merge the changes into that entity.
<T> T
save(T entity)
          If an entity with the same ID already exists in the database, merge the changes into that entity.
 List search(ISearch search)
          Search for objects given the search parameters in the specified ISearch object.
 SearchResult searchAndCount(ISearch search)
          Returns a SearchResult object that includes both the list of results like search() and the total length like count().
 Object searchUnique(ISearch search)
          Search for a single result using the given parameters.
 void setGeneralDAO(GeneralDAO generalDAO)
          GeneralDAO has default implementations for the standard DAO methods.
 
Methods inherited from class com.googlecode.genericdao.dao.BaseDAODispatcher
callMethod, callMethod, getSpecificDAO, getTypeFromArray, getUniformArrayType, setSpecificDAOs
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

generalDAO

protected GeneralDAO generalDAO
Constructor Detail

DAODispatcher

public DAODispatcher()
Method Detail

setGeneralDAO

public void setGeneralDAO(GeneralDAO generalDAO)
GeneralDAO has default implementations for the standard DAO methods. Which model class it uses is specified when calling the particular method.


count

public int count(ISearch search)
Description copied from interface: GeneralDAO
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 GeneralDAO

find

public <T> T find(Class<T> type,
                  Serializable id)
Description copied from interface: GeneralDAO

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

If none is found, return null.

Specified by:
find in interface GeneralDAO

find

public <T> T[] find(Class<T> type,
                    Serializable... ids)
Description copied from interface: GeneralDAO
Get all entities of the specified type from the datastore that have one of these ids. 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.

Specified by:
find in interface GeneralDAO

findAll

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

Specified by:
findAll in interface GeneralDAO

flush

public void flush()
Deprecated. use flush(Class)

Description copied from interface: GeneralDAO
Flushes changes in the Hibernate session to the datastore.

Specified by:
flush in interface GeneralDAO

flush

public void flush(Class<?> klass)

getReference

public <T> T getReference(Class<T> type,
                          Serializable id)
Description copied from interface: GeneralDAO

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 GeneralDAO

getReferences

public <T> T[] getReferences(Class<T> type,
                             Serializable... ids)
Description copied from interface: GeneralDAO

Get a reference to the entities of the specified type with the given ids from the datastore. An array of entities is returned that matches the same order of the ids listed in the call.

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 GeneralDAO

isAttached

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

Specified by:
isAttached in interface GeneralDAO

refresh

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

Specified by:
refresh in interface GeneralDAO

remove

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

Specified by:
remove in interface GeneralDAO
Returns:
true if the entity is found in the datastore and removed, false if it is not found.

remove

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

Specified by:
remove in interface GeneralDAO

removeById

public boolean removeById(Class<?> type,
                          Serializable id)
Description copied from interface: GeneralDAO
Remove the entity with the specified type and id from the datastore.

Specified by:
removeById in interface GeneralDAO
Returns:
true if the entity is found in the datastore and removed, false if it is not found.

removeByIds

public void removeByIds(Class<?> type,
                        Serializable... ids)
Description copied from interface: GeneralDAO
Remove all the entities of the given type from the datastore that have one of these ids.

Specified by:
removeByIds in interface GeneralDAO

save

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

save

public Object[] save(Object... entities)
Description copied from interface: GeneralDAO

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 GeneralDAO
Returns:
an array containing each managed entity corresponding to the entities passed in.

search

public List search(ISearch search)
Description copied from interface: GeneralDAO
Search for objects given the search parameters in the specified ISearch object.

Specified by:
search in interface GeneralDAO

searchAndCount

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

Specified by:
searchAndCount in interface GeneralDAO

searchUnique

public Object searchUnique(ISearch search)
Description copied from interface: GeneralDAO
Search for a single result using the given parameters.

Specified by:
searchUnique in interface GeneralDAO

getFilterFromExample

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

Specified by:
getFilterFromExample in interface GeneralDAO

getFilterFromExample

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

Specified by:
getFilterFromExample in interface GeneralDAO

merge

public <T> T merge(T entity)
Description copied from interface: GeneralDAO

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 GeneralDAO

merge

public Object[] merge(Object... entities)
Description copied from interface: GeneralDAO

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 GeneralDAO

persist

public void persist(Object... entities)
Description copied from interface: GeneralDAO

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 GeneralDAO


Copyright © 2008-2011. All Rights Reserved.