org.appfuse.dao.jpa
Class GenericDaoJpa<T,PK extends Serializable>

java.lang.Object
  extended by org.appfuse.dao.jpa.GenericDaoJpa<T,PK>
Type Parameters:
T - a type variable
PK - the primary key for that type
All Implemented Interfaces:
GenericDao<T,PK>
Direct Known Subclasses:
RoleDaoJpa, UserDaoJpa

public class GenericDaoJpa<T,PK extends Serializable>
extends Object
implements GenericDao<T,PK>

This class serves as the Base class for all other DAOs - namely to hold common CRUD methods that they might all use. You should only need to extend this class when your require custom CRUD logic.

To register this class in your Spring context file, use the following XML.

      <bean id="fooDao" class="org.appfuse.dao.hibernate.GenericDaoJpaHibernate">
          <constructor-arg value="org.appfuse.model.Foo"/>
          <property name="sessionFactory" ref="sessionFactory"/>
      </bean>
 

Author:
Bryan Noll Updated by jgarcia: update hibernate3 to hibernate4, jgarcia (update: added full text search + reindexing)

Field Summary
protected  org.apache.commons.logging.Log log
          Log variable for all child classes.
static String PERSISTENCE_UNIT_NAME
           
 
Constructor Summary
GenericDaoJpa(Class<T> persistentClass)
          Constructor that takes in a class to see which type of entity to persist.
GenericDaoJpa(Class<T> persistentClass, javax.persistence.EntityManager entityManager)
          Constructor that takes in a class to see which type of entity to persist.
 
Method Summary
 boolean exists(PK id)
          Checks for existence of an object of type T using the id arg.
 T get(PK id)
          Generic method to get an object based on class and identifier.
 List<T> getAll()
          Generic method used to get all objects of a particular type.
 List<T> getAllDistinct()
          Gets all records without duplicates.
 javax.persistence.EntityManager getEntityManager()
           
 void reindex()
          Generic method to regenerate full text index of the persistent class T
 void reindexAll(boolean async)
          Generic method to regenerate full text index of all indexed classes
 void remove(PK id)
          Generic method to delete an object
 void remove(T object)
          Generic method to delete an object
 T save(T object)
          Generic method to save an object - handles both update and insert.
 List<T> search(String searchTerm)
          Gets all records that match a search term.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected final org.apache.commons.logging.Log log
Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging


PERSISTENCE_UNIT_NAME

public static final String PERSISTENCE_UNIT_NAME
See Also:
Constant Field Values
Constructor Detail

GenericDaoJpa

public GenericDaoJpa(Class<T> persistentClass)
Constructor that takes in a class to see which type of entity to persist. Use this constructor when subclassing or using dependency injection.

Parameters:
persistentClass - the class type you'd like to persist

GenericDaoJpa

public GenericDaoJpa(Class<T> persistentClass,
                     javax.persistence.EntityManager entityManager)
Constructor that takes in a class to see which type of entity to persist. Use this constructor when subclassing or using dependency injection.

Parameters:
persistentClass - the class type you'd like to persist
entityManager - the configured EntityManager for JPA implementation.
Method Detail

getEntityManager

public javax.persistence.EntityManager getEntityManager()

getAll

public List<T> getAll()
Generic method used to get all objects of a particular type. This is the same as lookup up all rows in a table.

Specified by:
getAll in interface GenericDao<T,PK extends Serializable>
Returns:
List of populated objects

getAllDistinct

public List<T> getAllDistinct()
Gets all records without duplicates.

Note that if you use this method, it is imperative that your model classes correctly implement the hashcode/equals methods

Specified by:
getAllDistinct in interface GenericDao<T,PK extends Serializable>
Returns:
List of populated objects

get

public T get(PK id)
Generic method to get an object based on class and identifier. An ObjectRetrievalFailureException Runtime Exception is thrown if nothing is found.

Specified by:
get in interface GenericDao<T,PK extends Serializable>
Parameters:
id - the identifier (primary key) of the object to get
Returns:
a populated object
See Also:
org.springframework.orm.ObjectRetrievalFailureException

exists

public boolean exists(PK id)
Checks for existence of an object of type T using the id arg.

Specified by:
exists in interface GenericDao<T,PK extends Serializable>
Parameters:
id - the id of the entity
Returns:
- true if it exists, false if it doesn't

save

public T save(T object)
Generic method to save an object - handles both update and insert.

Specified by:
save in interface GenericDao<T,PK extends Serializable>
Parameters:
object - the object to save
Returns:
the persisted object

remove

public void remove(T object)
Generic method to delete an object

Specified by:
remove in interface GenericDao<T,PK extends Serializable>
Parameters:
object - the object to remove

remove

public void remove(PK id)
Generic method to delete an object

Specified by:
remove in interface GenericDao<T,PK extends Serializable>
Parameters:
id - the identifier (primary key) of the object to remove

search

public List<T> search(String searchTerm)
               throws SearchException
Description copied from interface: GenericDao
Gets all records that match a search term. "*" will get them all.

Specified by:
search in interface GenericDao<T,PK extends Serializable>
Parameters:
searchTerm - the term to search for
Returns:
the mathing records
Throws:
SearchException

reindex

public void reindex()
Generic method to regenerate full text index of the persistent class T

Specified by:
reindex in interface GenericDao<T,PK extends Serializable>

reindexAll

public void reindexAll(boolean async)
Generic method to regenerate full text index of all indexed classes

Specified by:
reindexAll in interface GenericDao<T,PK extends Serializable>
Parameters:
async - true to perform the reindexing asynchronously


Copyright © 2003-2012. All Rights Reserved.