import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import com.test.common.util.PaginationSupport;
@SuppressWarnings("unchecked")
public class BaseDao<T, ID extendsSerializable> extends HibernateDaoSupport
implements IBaseDao<T, ID>{
private Log logger =LogFactory.getLog(getClass());
protectedClass<T> entityClass;
public BaseDao() {
}
protected Class getEntityClass() {
if (entityClass == null){
entityClass =(Class<T>) ((ParameterizedType)getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
logger.debug("Tclass = " + entityClass.getName());
}
return entityClass;
}
public void saveOrUpdate(T t) throwsDataAccessException {
this.getHibernateTemplate().saveOrUpdate(t);
}
public T load(ID id) throwsDataAccessException {
T load = (T)getHibernateTemplate().load(getEntityClass(), id);
return load;
}
public T get(ID id) throws DataAccessException{
T load = (T)getHibernateTemplate().get(getEntityClass(), id);
return load;
}
public boolean contains(T t) throwsDataAccessException {
returngetHibernateTemplate().contains(t);
}
public void delete(T t, LockMode lockMode)throws DataAccessException {
getHibernateTemplate().delete(t,lockMode);
}
public void delete(T t) throwsDataAccessException {
getHibernateTemplate().delete(t);
}
public voiddeleteAll(Collection<T> entities)throws DataAccessException {
getHibernateTemplate().deleteAll(entities);
}
public List<T>find(String queryString, Object value)
throwsDataAccessException {
List<T>find = (List<T>)getHibernateTemplate().find(queryString, value);
return find;
}
public List<T>find(String queryString, Object[] values)
throwsDataAccessException {
List<T>finds = (List<T>)getHibernateTemplate().find(queryString,values);
return finds;
}
public List<T>find(String queryString) throws DataAccessException {
return(List<T>)getHibernateTemplate().find(queryString);
}
public void refresh(T t, LockMode lockMode)throws DataAccessException {
getHibernateTemplate().refresh(t,lockMode);
}
public void refresh(T t) throwsDataAccessException {
getHibernateTemplate().refresh(t);
}
public Serializable save(T t) throwsDataAccessException {
returngetHibernateTemplate().save(t);
}
public voidsaveOrUpdateAll(Collection<T>entities)
throwsDataAccessException {
getHibernateTemplate().saveOrUpdateAll(entities);
}
public void update(T t, LockMode lockMode)throws DataAccessException {
getHibernateTemplate().update(t,lockMode);
}
public void update(T t) throwsDataAccessException {
getHibernateTemplate().update(t);
}
public List<T>list() throws DataAccessException {
returngetHibernateTemplate().loadAll(getEntityClass());
}
public List<T>findByNamedQuery(String queryName)
throwsDataAccessException {
returngetHibernateTemplate().findByNamedQuery(queryName);
}
public List<T>findByNamedQuery(String queryName, Object value)
throwsDataAccessException {
returngetHibernateTemplate().findByNamedQuery(queryName, value);
}
public List<T>findByNamedQuery(String queryName, Object[] values)
throwsDataAccessException {
returngetHibernateTemplate().findByNamedQuery(queryName, values);
}
public PaginationSupportfindPageByCriteria(
finalDetachedCriteria detachedCriteria, final int pageSize,
final intstartIndex) {
return (PaginationSupport)getHibernateTemplate().execute(
newHibernateCallback() {
publicObject doInHibernate(org.hibernate.Session session)
throwsHibernateException {
Criteriacriteria = detachedCriteria
.getExecutableCriteria(session);
inttotalCount = ((Integer) criteria.setProjection(
Projections.rowCount()).uniqueResult())
.intValue();
criteria.setProjection(null);
Listitems = criteria.setFirstResult(startIndex)
.setMaxResults(pageSize).list();
PaginationSupportps = new PaginationSupport(items,
totalCount,pageSize, startIndex);
returnps;
}
},true);
}
public PaginationSupportfindPageByQuery( final String hql, final StringcountHql,final int pageSize,final int startIndex){
return (PaginationSupport)getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Sessionsession) throws HibernateException, SQLException {
inttotalCount=((Integer)session.createQuery(countHql).iterate().next()).intValue();
Queryquery = session.createQuery(hql);
query.setFirstResult(startIndex);
query.setMaxResults(pageSize);
List items = query.list();
PaginationSupport ps = new PaginationSupport(items,
totalCount, pageSize, startIndex);
return ps;
}
},true);
}
}