net.ramapuram.thomas.dao.hibernate
Class GenericDaoHibernate<T,PK extends java.io.Serializable>

java.lang.Object
  extended by net.ramapuram.thomas.dao.hibernate.GenericDaoHibernate<T,PK>
Type Parameters:
T - a type variable
PK - the primary key for that type
All Implemented Interfaces:
GenericDao<T,PK>
Direct Known Subclasses:
RoleDaoHibernate, UserDaoHibernate

public class GenericDaoHibernate<T,PK extends java.io.Serializable>
extends java.lang.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="net.ramapuram.thomas.dao.hibernate.GenericDaoHibernate">
          <constructor-arg value="net.ramapuram.thomas.model.Foo"/>
      </bean>
 

Author:
Bryan Noll

Field Summary
protected  org.apache.commons.logging.Log log
          Log variable for all child classes.
 
Constructor Summary
GenericDaoHibernate(java.lang.Class<T> persistentClass)
          Constructor that takes in a class to see which type of entity to persist.
GenericDaoHibernate(java.lang.Class<T> persistentClass, org.hibernate.SessionFactory sessionFactory)
          Constructor that takes in a class and sessionFactory for easy creation of DAO.
 
Method Summary
 boolean exists(PK id)
          Checks for existence of an object of type T using the id arg.
 java.util.List<T> findByNamedQuery(java.lang.String queryName, java.util.Map<java.lang.String,java.lang.Object> queryParams)
          Find a list of records by using a named query
 T get(PK id)
          Generic method to get an object based on class and identifier.
 java.util.List<T> getAll()
          Generic method used to get all objects of a particular type.
 java.util.List<T> getAllDistinct()
          Gets all records without duplicates.
 org.springframework.orm.hibernate3.HibernateTemplate getHibernateTemplate()
           
 org.hibernate.SessionFactory getSessionFactory()
           
 void remove(PK id)
          Generic method to delete an object based on class and id
 T save(T object)
          Generic method to save an object - handles both update and insert.
 void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
           
 
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

Constructor Detail

GenericDaoHibernate

public GenericDaoHibernate(java.lang.Class<T> persistentClass)
Constructor that takes in a class to see which type of entity to persist. Use this constructor when subclassing.

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

GenericDaoHibernate

public GenericDaoHibernate(java.lang.Class<T> persistentClass,
                           org.hibernate.SessionFactory sessionFactory)
Constructor that takes in a class and sessionFactory for easy creation of DAO.

Parameters:
persistentClass - the class type you'd like to persist
sessionFactory - the pre-configured Hibernate SessionFactory
Method Detail

getHibernateTemplate

public org.springframework.orm.hibernate3.HibernateTemplate getHibernateTemplate()

getSessionFactory

public org.hibernate.SessionFactory getSessionFactory()

setSessionFactory

public void setSessionFactory(org.hibernate.SessionFactory sessionFactory)

getAll

public java.util.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 java.io.Serializable>
Returns:
List of populated objects

getAllDistinct

public java.util.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 java.io.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 java.io.Serializable>
Parameters:
id - the identifier (primary key) of the object to get
Returns:
a populated object
See Also:
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 java.io.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 java.io.Serializable>
Parameters:
object - the object to save
Returns:
the persisted object

remove

public void remove(PK id)
Generic method to delete an object based on class and id

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

findByNamedQuery

public java.util.List<T> findByNamedQuery(java.lang.String queryName,
                                          java.util.Map<java.lang.String,java.lang.Object> queryParams)
Find a list of records by using a named query

Specified by:
findByNamedQuery in interface GenericDao<T,PK extends java.io.Serializable>
Parameters:
queryName - query name of the named query
queryParams - a map of the query names and the values
Returns:
a list of the records found


Copyright © 2011. All Rights Reserved.