商业支持|产品购买|技术支持|关于我们|联系我们

当前位置: SHOP++网店系统 > 技术支持 >

hibernate 泛型DAO

时间:2009-12-01 11:49来源:未知
package net.payj.base; import static org.hibernate.EntityMode.POJO; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import net.payj.common.bean.Page; import net.payj.util.Ref
  

package net.payj.base;

import static org.hibernate.EntityMode.POJO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.payj.common.bean.Page;
import net.payj.util.ReflectionUtil;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.impl.CriteriaImpl;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.transform.ResultTransformer;
import org.springframework.stereotype.Repository;

@Repository
public class BaseDaoImpl<T, PK extends Serializable> implements BaseDao<T, PK> {

 private Class<T> entityClass;
 protected SessionFactory sessionFactory;

 public BaseDaoImpl() {
  this.entityClass = ReflectionUtil.getSuperClassGenricType(getClass());
 }

 @Resource
 public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }

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

 @SuppressWarnings("unchecked")
 public T get(PK id) {
  return (T) getSession().get(entityClass, id);
 }

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

 @SuppressWarnings("unchecked")
 public List<T> findByHql(String hql, Object... values) {
  return createQuery(hql, values).list();
 }

 @SuppressWarnings("unchecked")
 public List<T> findAll() {
  String hql = "from " + entityClass.getName();
  return createQuery(hql).list();
 }

 @SuppressWarnings("unchecked")
 public List<T> findAll(boolean isCacheable) {
  String hql = "from " + entityClass.getName();
  return createQuery(hql).setCacheable(isCacheable).list();
 }

 @SuppressWarnings("unchecked")
 public List<T> findAllByCache() {
  String cacheRegion = "eternalCacheRegion";// 设置cacheRegion
  String hql = "from " + entityClass.getName();
  return createQuery(hql).setCacheable(true).setCacheRegion(cacheRegion).list();
 }

 @SuppressWarnings("unchecked")
 public List findAllProperty(String propertyName) {
  String hql = "select model." + propertyName + " from " + entityClass.getName() + " model";
  Query query = getSession().createQuery(hql);
  return query.list();
 }

 public List<T> findByIds(PK[] ids) {
  List<T> list = new ArrayList<T>();
  for (PK id : ids) {
   T entity = load(id);
   if (entity != null) {
    list.add(entity);
   }
  }
  return list;
 }

 public List<T> findByProperty(String propertyName, Object value) {
  String hql = "from " + entityClass.getName() + " as model where model." + propertyName + "=?";
  return findByHql(hql, value);
 }

 @SuppressWarnings("unchecked")
 public T findUniqueByProperty(String propertyName, Object value) {
  String hql = "from " + entityClass.getName() + " as model where model." + propertyName + "=?";
  return (T) createQuery(hql, value).uniqueResult();
 }

 public int findTotalCount() {
  String hql = "select count(*) from " + entityClass.getName();
  return Integer.parseInt(createQuery(hql).uniqueResult().toString());
 }

 public boolean isExist(String propertyName, Object value) {
  List<T> list = findByProperty(propertyName, value);

推荐内容