CommonDAO.java~4~
资源名称:shihua.rar [点击查看]
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:10k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
JavaScript
- package com.chinacannel.common;
- import java.util.List;
- import java.util.ArrayList;
- import net.sf.hibernate.*;
- import net.sf.hibernate.type.Type;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import java.io.Serializable;
- public class CommonDAO {
- private static Log log = LogFactory.getLog(CommonDAO.class);
- public CommonDAO() {
- }
- public int getCount(Class cls) throws DAOException {
- String hql = "select count(*) from " + cls.getName() + " s";
- return this.getCountBySql(hql);
- }
- public int getCountBySql(String hql) throws DAOException {
- Session session = null;
- int count = 0;
- try {
- session = HibernateUtil.currentSession();
- count = ((Integer) session.iterate(hql).next()).intValue();
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- }
- return count;
- }
- public boolean createObject(Object object) throws DAOException {
- boolean lpReturnValue = false;
- log.debug("创建对象的实例:" + object.getClass().toString());
- Session session = null;
- //Transaction trans = null;
- try {
- session = HibernateUtil.currentSession();
- // trans = session.beginTransaction();
- session.save(object);
- session.flush();
- // trans.commit();
- lpReturnValue = true;
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return lpReturnValue;
- }
- public boolean updateObject(Object object) throws DAOException {
- boolean lpReturnValue = false;
- log.debug("更新对象的实例:" + object.getClass().toString());
- Session session = null;
- //Transaction trans = null;
- try {
- session = HibernateUtil.currentSession();
- //trans = session.beginTransaction();
- session.update(object);
- session.flush();
- //trans.commit();
- lpReturnValue = true;
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return lpReturnValue;
- }
- public boolean removeObject(Object object) throws DAOException {
- boolean lpReturnValue = false;
- log.debug("删除对象的实例:" + object.getClass().toString());
- Session session = null;
- Transaction trans = null;
- try {
- session = HibernateUtil.currentSession();
- trans = session.beginTransaction();
- session.delete(object);
- trans.commit();
- //session.flush();
- lpReturnValue = true;
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- try {
- trans.rollback();
- } catch (HibernateException he2) {
- //
- }
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return lpReturnValue;
- }
- public Object loadObject(Serializable id, Class cls) throws
- DAOException {
- log.debug("获得对象的实例:" + cls.toString() + ",关键值为" + id.toString());
- Object po = null;
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- po = (Object) session.load(cls, id);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return po;
- }
- public Object loadOneObjectBySQL(String hql) throws DAOException {
- List list = this.loadObjectListBySQL(hql, 0, 1);
- if (list.size() > 0) {
- return list.get(0);
- } else {
- return null;
- }
- }
- public List loadObjectBySQL(String hql, Object[] params, Type[] types) throws
- DAOException {
- List ls = new ArrayList(0);
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- ls = session.find(hql, params, types);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- }
- return ls;
- }
- public List loadObjectListBySQL(String hql) throws DAOException {
- List ls = new ArrayList(0);
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- ls = session.find(hql);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // }finally{
- // try{
- // HibernateUtil.closeSession();
- // }catch(Exception ex){
- // log.error(ex.getMessage(), ex);
- // }
- }
- return ls;
- }
- public List loadObjectListBySQL(String hql, Object[] params, Type[] types,
- int start, int top) throws DAOException {
- List ls = new ArrayList(0);
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- Query query = session.createQuery(hql);
- for (int i = 0; i < params.length; i++) {
- query.setParameter(i, params[i], types[i]);
- }
- query.setFirstResult(start);
- query.setMaxResults(top);
- ls = query.list();
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // }finally{
- // try{
- // HibernateUtil.closeSession();
- // }catch(Exception ex){
- // log.error(ex.getMessage(), ex);
- // }
- }
- return ls;
- }
- public List loadObjectListBySQL(String hql, int start, int top) throws
- DAOException {
- List ls = new ArrayList(0);
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- Query query = session.createQuery(hql);
- query.setFirstResult(start);
- query.setMaxResults(top);
- ls = query.list();
- //}
- // catch (UnresolvableObjectException uoe) {
- // log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // }finally{
- // try{
- // HibernateUtil.closeSession();
- // }catch(Exception ex){
- // log.error(ex.getMessage(), ex);
- // }
- }
- return ls;
- }
- public List loadAllObjects(Class cls) throws DAOException {
- List ls = null;
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- ls = session.find(getQuerySQL(cls));
- if (ls == null)
- ls = new ArrayList(0);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return ls;
- }
- public int executeDelete(String hql) throws DAOException{
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- return session.delete(hql);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- }
- return -1;
- }
- protected String getQuerySQL(Class cls) {
- log.debug("from " + cls.getName() + " s");
- return "from " + cls.getName() + " s";
- }
- }