GenericDAO定义
package com.vwintech.edms.persistent.dao;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
public class GenericDAO
{
protected static final Log log = LogFactory.getLog(GenericDAO.class);
protected SessionFactory sessionFactory;
protected Class entityClass;

public GenericDAO(SessionFactory sessionFactory, Class entityClass)
{
  this.sessionFactory = sessionFactory;
  this.entityClass = entityClass;
}

public void save(T entity)
{
  getSession().save(entity);
}


public void update(T entity)
{
  getSession().update(entity);
}

public void delete(T entity)
{
  getSession().delete(entity);
}

public void delete(String id)
{
  delete(findById(id));
}

public List findAll()
{
  return findByCriteria();
}

@SuppressWarnings("unchecked")
public T findById(final String id)
{
  return (T) getSession().load(entityClass, id);
}

public List find(String hql, Object... values)
{
  return createQuery(hql, values).list();
}

public Object findUnique(String hql, Object... values)
{
  return createQuery(hql, values).uniqueResult();
}

public Long findLong(String hql, Object... values)
{
  return (Long) findUnique(hql, values);
}

@SuppressWarnings("unchecked")
public List findByCriteria(Criterion... criterion)
{
  return createCriteria(criterion).list();
}

@SuppressWarnings("unchecked")
public List findByProperty(String propertyName, Object value)
{
  return createCriteria(Restrictions.eq(propertyName, value)).list();
}

@SuppressWarnings("unchecked")
public T findUniqueByProperty(String propertyName, Object value)
{
  return (T) createCriteria(Restrictions.eq(propertyName, value))
    .uniqueResult();
}

public Query createQuery(String queryString, Object... values)
{
  Query queryObject = getSession().createQuery(queryString);
  if (values != null)
  {
   for (int i = 0; i < values.length; i++)
   {
    queryObject.setParameter(i, values);
   }
  }
  return queryObject;
}

public Criteria createCriteria(Criterion... criterions)
{
  Criteria criteria = getSession().createCriteria(entityClass);
  for (Criterion c : criterions)
  {
   criteria.add(c);
  }
  return criteria;
}


public List findBySQL(String sql)
{
  return getSQLQuery(sql).list();
}


@SuppressWarnings("unchecked")
public List findBySQLAsEntity(String sql)
{
  SQLQuery query = getSQLQuery(sql);
  query.addEntity(entityClass);
  return query.list();
}


public SQLQuery getSQLQuery(String sql)
{
  return getSession().createSQLQuery(sql);
}


public Session getSession()
{
  return sessionFactory.getCurrentSession();
}

public SessionFactory getSessionFactory()
{
  return sessionFactory;
}
}

service调用类
private GenericDAO moduleDAO;
private GenericDAO opblogDAO;

@SuppressWarnings("unchecked")
public void setSessionFactory(SessionFactory sessionFactory)
{
  moduleDAO = new GenericDAO(sessionFactory,Module.class);
  opblogDAO = new GenericDAO(sessionFactory,Opblog.class);
}