欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

--BaseHibernateDao-高级 博客分类: Hibernate HibernateF#DAOORM.net 

程序员文章站 2024-02-14 19:37:34
...
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();
   }
  }
 }
}