--BaseHibernateDao-高级

package net.music.util;

import java.lang.reflect.Field;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * 
 * @author kkllmey Jul 7, 2008
 */
public class BaseHibernateDao extends HibernateDaoSupport {


 protected void save(Object entity) throws Exception {
  try {
   super.getHibernateTemplate().save(entity);
  } catch (DataAccessException e) {
   throw new Exception("save entity error kkll", e);
  }
 }


 protected void update(Object entity) throws Exception {
  try {
   super.getHibernateTemplate().update(entity);
  } catch (RuntimeException e) {
   throw new Exception("update entity error kkll", e);
  }
 }


 protected void delete(Object entity) throws Exception {
  try {
   super.getHibernateTemplate().delete(entity);
  } catch (RuntimeException e) {
   throw new Exception("del entity error kkll", e);
  }
 }


 protected <T> void delete(Class<T> cls, java.io.Serializable id)
   throws Exception {
  try {
   Object obj = this.get(cls, Integer.parseInt(id.toString()));
   super.getHibernateTemplate().delete(obj);
  } catch (Exception e) {
   throw new Exception("del entity error kkll", e);
  }
 }

 protected <T> T get(Class<T> cls, java.io.Serializable id) throws Exception {
  try {
   return (T) super.getHibernateTemplate().get(cls, id);
  } catch (RuntimeException e) {
   throw new Exception("get entity error kkll");
  }
 }


 @SuppressWarnings("unchecked")
 protected <T> List<T> search(Object entity, Class<T> cls, int pageSize,
   int currPage, boolean isASC, String ascKey, String... joins)
   throws Exception {
  try {
   Session session = super.getSession();
   Criteria cr = session.createCriteria(cls);
   
   for (String join : joins) {
    cr.setFetchMode(join, FetchMode.JOIN);
   }
   if (!"".equals(ascKey) && null != ascKey) {
    if (isASC) {
     cr.addOrder(Order.asc(ascKey));
    } else {
     cr.addOrder(Order.desc(ascKey));
    }
   }
   if (null != entity) {
    cr.add(Example.create(entity));
    fillCriteria(cr, entity);
   }
   cr.setFirstResult((currPage - 1) * pageSize);
   cr.setMaxResults(pageSize);
   List<T> lst = cr.list();
   //session.close();
   return lst;
  } catch (Exception e) {
   throw new Exception("exe hql error kkll", e);
  }
 }

 protected void fillCriteria(Criteria cr, Object entity) {
  Field[] fs = entity.getClass().getDeclaredFields();
  try {
   for (Field f : fs) {
    f.setAccessible(true);
    String temp = f.getType().getName();
    if (temp.indexOf("java.") == -1) {
     if (null != f.get(entity) && !"".equals(entity)) {
      if(f.get(entity).getClass().getDeclaredFields()[0].get(f.get(entity))!=null)
       cr.add(Restrictions.like(f.getName(), f.get(entity)));
     }
    }
    f.setAccessible(false);
   }
  } catch (SecurityException e) {
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  }
 }


 protected <T> List<T> exeHql(String hql) throws Exception {
  try {
   Session session = super.getSession();
   List<T> lst = session.createQuery(hql).list();
   session.close();
   return lst;
  } catch (Exception e) {
   throw new Exception("exe hql error kkll", e);
  }
 }
 
 protected <T> List<T> exeHql(String hql, int currPage, int pageSize,
   List... paras) throws Exception {
  try {
   Session session = super.getSession();
   Query q = session.createQuery(hql);
   if(null!=paras&&0<paras.length)
    setParas(q, paras[0]);
   q.setFirstResult((currPage - 1) * pageSize);
   q.setMaxResults(pageSize);
   List<T> lst = q.list();
   session.close();
   return lst;
  } catch (Exception e) {
   throw new Exception("exe hql error kkll", e);
  }
 }
 protected void setParas(Query q, List paras) {
  if (paras != null && 0 < paras.size()) {
   try {
    for (int i = 0; i < paras.size(); i++) {
     String para = paras.get(i).getClass().getName();
     int lasxI=para.lastIndexOf(".")+1;
     String name = "set"+para.substring(lasxI,para.length());
     if(paras.get(i).getClass()==Integer.class)
      q.getClass().getMethod(name,int.class,int.class).invoke(q,i,paras.get(i));
     else
      q.getClass().getMethod(name,int.class,paras.get(i).getClass()).invoke(q,i,paras.get(i));
    }
   } catch (Exception ex) {
    ex.printStackTrace();
   }
  }
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值