--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(); } } } }
推荐阅读
-
--BaseHibernateDao-高级 博客分类: Hibernate HibernateF#DAOORM.net
-
--BaseHibernateDao-高级 博客分类: Hibernate HibernateF#DAOORM.net
-
Hibernate唯一约束配置 博客分类: Hibernate Hibernate
-
对于hibernate缓存的简单理解 博客分类: hibernate HibernateSpringAOPCache
-
[转]JPA(Hibernate)不生成外键 博客分类: JPA
-
[转]JPA(Hibernate)不生成外键 博客分类: JPA
-
Eclipse3M7+Hibernate2 运行环境测试 博客分类: JEE开发 HibernatePostgreSQL.netSQLQQ
-
Hibernate: Null value was assigned to a property of primitive type setter of 博客分类: JavaEE报错调试(已解决) hibernate实体类
-
hibernate实体类 博客分类: hibernate实体类 hibernate实体类属性
-
HibernateDaoSupport update delete不执行 博客分类: hibernate upda hibernatedaoupdate不执行