【转】HibernateDaoImpl
原文链接:https://www.javatt.com/p/96866
package com.oa.common.hibernate;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.oa.common.utils.PageParam;
import com.oa.common.utils.PageUtil;
import com.oa.common.utils.StringUtil;
/**
* 扩展HibernateDaoSupport的泛型基类
*
* @param <T>
* 实体类型
*/
@SuppressWarnings("all")
public class HibernateDaoImpl extends HibernateDaoSupport implements HibernateDao {
/**
* 删除对象
*
* @param entity
* 实体类
*/
@Override
public void delete(final Object entity) {
getSession().delete(entity);
}
/**
* 根据实体类与ID删除对象
*
* @param clazz
* 实体类
* @param id
* 主键ID
*/
@Override
public void delete(final Class<?> clazz, final Serializable id) {
delete(get(clazz, id));
}
/**
* 获取所有数据
*
* @param entityClass
* 参数T的反射类型
*/
@Override
public <X> List<X> getAll(final Class<X> entityClass) {
return createCriteria(entityClass).list();
}
@Override
public void save(Object transientInstance) {
getHibernateTemplate().save(transientInstance);
}
@Override
public void saveOrUpdate(Object transientInstance) {
getHibernateTemplate().saveOrUpdate(transientInstance);
}
@Override
public void update(Object transientInstance) {
getHibernateTemplate().update(transientInstance);
}
@Override
public Object merge(Object transientInstance) {
return getHibernateTemplate().merge(transientInstance);
}
private boolean isNull(Serializable id) {
if (id == null || id.toString().trim().length() < 1)
return true;
if (("Long".equals(id.getClass().getSimpleName()) || "Integer".equals(id.getClass().getSimpleName())) && Long.valueOf(id.toString()) == 0) {
return true;
}
return false;
}
/**
* 根据实体类与ID获得对象
*
* @param clazz
* 实体类
* @param id
* 主键ID
*/
@Override
public <T> T load(Class<T> entityClass, Serializable id) {
if (isNull(id))
return null;
return (T) getHibernateTemplate().load(entityClass, id);
}
/**
* 根据实体类与ID获得对象
*
* @param clazz
* 实体类
* @param id
* 主键ID
*/
@Override
public <T> T get(Class<T> entityClass, Serializable id) {
if (isNull(id))
return null;
return (T) getHibernateTemplate().get(entityClass, id);
}
/**
* HQL方式查询(不建议使用)
*
* @param hql
* 符合HQL语法的查询语句
* @param values
* 数量可变的条件值,按顺序绑定
*/
public <T> List<T> queryByHQL(final String hql, final Object... values) {
Query query = getSession().createQuery(hql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return (List<T>) query.list();
}
/**
* HQL方式查询(建议使用)
*
* @param hql
* 符合HQL语法的查询语句
* @param values
*/
public <T> List<T> queryByHQL(final String hql, final Map<String, Object> params) {
Query query = getSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
query.setParameter(key, params.get(key));
}
}
return (List<T>) query.list();
}
/**
* SQL方式查询
*
* @param sql
* 符合SQL语法的查询语句
* @param values
* 数量可变的条件值,按顺序绑定
*/
public <T> List<T> queryBySQL(final Class<T> clazz, final String sql, final Object... values) {
SQLQuery query = getSession().createSQLQuery(sql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return (List<T>) query.addEntity(clazz).list();
}
/**
* 根据类型创建查询对象
*
* @param clazz
* 类型
*/
public Criteria createCriteria(final Class clazz) {
return getSession().createCriteria(clazz);
}
/**
* 对象化查询
*
* @param entityClass
* 参数T的反射类型
* @param criterions
* 数量可变的Criterion
*/
public Criteria createCriteria(final Class clazz, final Criterion... criterions) {
Criteria criteria = getSession().createCriteria(clazz);
for (Criterion c : criterions) {
criteria.add(c);
}
return criteria;
}
public <T> List<T> findByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
});
}
// 用于分页处理
public <T> List<T> findByCriteria(final DetachedCriteria detachedCriteria, final int pageSize, final int currentPage) {
// 返回记录总数
int total = this.findRowCountByCriteria(detachedCriteria);
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
if (pageSize > 0) {
criteria.setMaxResults(pageSize);
}
if (currentPage > 0) {
criteria.setFirstResult(currentPage);
}
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
});
}
// 用于分页处理
public List findByCriteriaPage(final DetachedCriteria detachedCriteria, final int pageSize, final int currentPage) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
if (pageSize > 0) {
criteria.setFirstResult((currentPage - 1) * pageSize);
criteria.setMaxResults(pageSize);
}
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
});
}
@Override
public <T> T findOne(final String hql, final Object... params) {
return (T) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query hqlQuery = session.createQuery(hql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
hqlQuery.setParameter(i, params[i]);
}
}
return hqlQuery.setMaxResults(1).uniqueResult();
}
});
}
public <T> T findOneCache(final String hql, final Object... params) {
return (T) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query hqlQuery = session.createQuery(hql).setCacheable(true);
if (params != null) {
for (int i = 0; i < params.length; i++) {
hqlQuery.setParameter(i, params[i]);
}
}
return hqlQuery.setMaxResults(1).uniqueResult();
}
});
}
@Override
public <T> T findOneSql(