📄 daosupporthibernate3imp.java
字号:
/**
*文件功能:利用泛型的基础DAO组件
*/
package com.common.dao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
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;
/**
* DaoSupport接口对Hibernate3的实现
* @作者 徐建协
* @日期 2008-1-23
*/
public class DaoSupportHibernate3Imp <T, ID extends Serializable> extends HibernateDaoSupport implements DaoSupport <T, ID> {
private Class<T> entityClass;
private Log log=LogFactory.getLog(DaoSupportHibernate3Imp.class);
@SuppressWarnings("unchecked")
public DaoSupportHibernate3Imp(){
entityClass =(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public DaoSupportHibernate3Imp(Class<T> t){
entityClass=t;
}
public void setEntityClass(Class<T> entityClass) {
this.entityClass = entityClass;
}
/********************
* 获取该DAO对应操作的实体bean的类型
* @return
*/
public Class<T> getEntityClass(){
return this.entityClass;
}
/*********
* 获取该DAO对应的实体bean的名词
* @return
*/
public String getEntityName(){
return this.getEntityClass().getName();
}
/********************
* 根据主键获取单个实体对象,如果返回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) {
log.error("数据库执行失败: " + e);
e.printStackTrace();
throw new DAOException(e);
}catch (Throwable e) {
log.error("系统错误: " + e);
e.printStackTrace();
throw new DAOException(e);
}
}
/**************************
* 删除实体对象数据
* @param entity
*/
public void removeEntity(T[] array){
try {
for(int i=0;i<array.length;i++){
if(i%Config.batchSize==0 &&i>0){
getHibernateTemplate().delete(array[i]);
getHibernateTemplate().flush();
getHibernateTemplate().clear();
}else{
getHibernateTemplate().delete(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);
}
}
public void removeEntity(ID id){
String hql="";
String[] tmpArray=getEntityName().split("[.]");
String modelName=tmpArray[tmpArray.length-1];
hql="delete from "+modelName+" where id=?";
try{
batchUpdateOrDelete(hql,new Object[]{id});
} catch (DataAccessException e) {
log.error("数据库执行失败: " + e);
e.printStackTrace();
throw new DAOException(e);
}catch (Throwable e) {
log.error("系统错误: " + e);
e.printStackTrace();
throw new DAOException(e);
}
}
public void removeEntity(ID[] array){
String hql="";
String[] tmpArray=getEntityName().split("[.]");
String modelName=tmpArray[tmpArray.length-1];
hql="delete from "+modelName+" where id=?";
try {
for(int i=0;i<array.length;i++){
if(i%Config.batchSize==0 &&i>0){
getHibernateTemplate().flush();
getHibernateTemplate().clear();
}
batchUpdateOrDelete(hql,new Object[]{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 hql
*/
public void batchUpdateOrDelete(final String hql,final Object[] values){
getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
try {
Query queryObject=session.createQuery(hql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
queryObject.executeUpdate();
} catch (HibernateException e) {
e.printStackTrace();
log.error("批量删除数据错误:" + e);
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);
}
return null;
}
},true);
}
/*********************
* 判断对象在数据库中是否存在
* @param
*/
public boolean isExists(T t,List<String> names){
boolean res=false;
Criteria criteria = getSession().createCriteria(getEntityClass()).setProjection(Projections.rowCount());
try {
for(int i=0;i<names.size();i++){
criteria.add(Restrictions.eq(names.get(i), BeanUtils.getProperty(t, names.get(i))));
}
} catch (Exception e) {
log.error(e.getMessage());
}
res=((Integer) criteria.uniqueResult()).intValue() > 0;
return res;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -