⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basedaohibernate.java

📁 使用WEBWORK,SPRING,HIBERNATE编写的简单的添加
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 *文件功能:利用泛型的基础DAO组件 
 */
package com.common.dao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.PreparedStatement;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.common.Config;
import com.common.exception.DAOException;
import com.common.util.BeanUtils;
import com.common.util.PaginationSupport;
/**
 * @作者 徐建协
 * @日期 2008-1-11
 */
abstract  public class  BaseDaoHibernate<T, ID extends Serializable> extends HibernateDaoSupport implements BaseDao <T, ID> {
	private Class<T> entityClass;
	private Log log=LogFactory.getLog(BaseDaoHibernate.class);
	@SuppressWarnings("unchecked") 
	public BaseDaoHibernate(){
		entityClass =(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
  
	}
	/********************
	 * 获取该DAO对应操作的实体bean的类型
	 * @return
	 */
	public Class<T> getEntityClass(){
		return this.entityClass;
	}
	/*********
	 * 获取该DAO对应的实体bean的名词
	 * @return
	 */
	public String getEntityName(){
		return this.getEntityClass().getName();
	}
	/********************
	 * 获取实体对象的所有数据   
	 * @return
	 */
	@SuppressWarnings("unchecked") 
	public List<T> findAllEntity(){
		return (List<T>)getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException { 
        		try {
        			return session.createCriteria(getEntityClass()).list();
        		} catch (HibernateException e) {
        			log.error("数据库中无该查询表:" + e);
        			e.printStackTrace();
        			throw new DAOException("数据库中无该查询表:"
        					+ e.getMessage());
        		} catch (DataAccessException e) {
        			e.printStackTrace();
        			log.error("数据库执行错误:" + e);
        			throw new DAOException(e);
        		} catch (Throwable e) {
        			e.printStackTrace();
        			log.error("系统错误:" + e);
        			throw new DAOException(e);
        		}                        
            } 		
		});
	}
	
	public List find(String hsql,Object...values){  
		try {
			if (values.length == 0)    
	            return getHibernateTemplate().find(hsql);    
	        else   
	            return getHibernateTemplate().find(hsql, values);
		} catch (DataAccessException e) {
			log.error("数据查询失败: " + e);
			e.printStackTrace();
			throw new DAOException(e);
		}catch (Throwable e) {
			e.printStackTrace();
			log.error("系统错误: " + e);
			throw new DAOException(e);
		}			
	}
	/********************
	 * 根据主键获取单个实体对象,如果返回null
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked") 
	public T getEntity(ID id){
		T entity;
		try {
			entity = (T)getHibernateTemplate().get(entityClass, id);
		} catch (DataAccessException e) {
			e.printStackTrace();
			log.error("不存在该数据: " + e);
			throw new DAOException(e);
		} catch (Throwable e) {
			e.printStackTrace();
			log.error("系统错误: " + e);
			throw new DAOException(e);
		}
		return entity;		
	}

	/********************
	 * 根据主键获取单个实体对象,如果没有抛出异常,在SESSION范围内有效,用到才去取数据的
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked") 
	public T loadEntity(ID id){
		T entity;
		try {
			entity = (T)getHibernateTemplate().load(entityClass, id);
		} catch (DataAccessException e) {
			e.printStackTrace();
			log.error("不存在该数据: " + e);
			throw new DAOException(e);
		} catch (Throwable e) {
			e.printStackTrace();
			log.error("系统错误: " + e);
			throw new DAOException(e);
		}
		return entity;		
	}
	/****************************
	 * 保存单个实体对象
	 * @param entity
	 * @return
	 */
	public Serializable saveEntity(T entity){
		Serializable res;
		try {
			
			res=getHibernateTemplate().save(entity);
		} catch (DataAccessException e) {
			log.error("数据库保存错误: " + e);
			e.printStackTrace();
			throw new DAOException(e);
		} catch (Throwable e) {
			e.printStackTrace();
			log.error("系统错误: " + e);
			throw new DAOException(e);
		}	
		return res;
	}
	/*******************************
	 * 更新单个实体对象
	 * @param entity
	 * @throws DataAccessException
	 */
	public void updateEntity(T entity){
		try {
			getHibernateTemplate().merge(entity);
		} catch (DataAccessException e) {
			e.printStackTrace();
			log.error("数据库更新失败: " + e);
			throw new DAOException(e);
		}catch (Throwable e) {
			e.printStackTrace();
			log.error("系统错误: " + e);
			throw new DAOException(e);
		}		
	}
	/******************************
	 * 批量数据的对象的更新
	 * @param array
	 */
	public void updateEntity(T[] array){
		try {
			for(int i=0;i<array.length;i++){
			  if(i%Config.batchSize==0 &&i>0){
				  getHibernateTemplate().merge(array[i]);
				  getHibernateTemplate().flush(); 
				  getHibernateTemplate().clear();
			  }else{
				  getHibernateTemplate().merge(array[i]);
			  }
			}
		} catch (DataAccessException e) {
			e.printStackTrace();
			log.error("数据库执行失败: " + e);
			throw new DAOException(e);
		}catch (Throwable e) {
			e.printStackTrace();
			log.error("系统错误: " + e);
			throw new DAOException(e);
		}
	}	
	/*********************
	 * 保存或者更新实体对象
	 * @param entity
	 */
	public void saveOrUpdateEntity(T entity){
		try {
			getHibernateTemplate().saveOrUpdate(entity);
		} catch (DataAccessException e) {
			log.error("数据库更新失败: " + e);
			e.printStackTrace();
			throw new DAOException(e);
		}catch (Throwable e) {
			e.printStackTrace();
			log.error("系统错误: " + e);
			throw new DAOException(e);
		}		
	}
	/******************************
	 * 批量数据的对象的保存
	 * @param array
	 */
	public void saveEntity(T[] array){
		try {
			for(int i=0;i<array.length;i++){
			  if(i%Config.batchSize==0 &&i>0){
				  getHibernateTemplate().save(array[i]);
				  getHibernateTemplate().flush(); 
				  getHibernateTemplate().clear();
			  }else{
				  getHibernateTemplate().save(array[i]);
			  }
			}
		} catch (DataAccessException e) {
			e.printStackTrace();
			log.error("数据库执行失败: " + e);
			throw new DAOException(e);
		}catch (Throwable e) {
			e.printStackTrace();
			log.error("系统错误: " + e);
			throw new DAOException(e);
		}
	}
	/**************************
	 * 删除实体对象数据
	 * @param entity
	 */
	public void removeEntity(T entity){
		try{
			getHibernateTemplate().delete(entity);
		} catch (DataAccessException e) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -