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

📄 persistencemanager.java~5~

📁 一个专门用来快速开发网站的框架
💻 JAVA~5~
📖 第 1 页 / 共 2 页
字号:
package com.core.persistence;

/**
 * 这个类通过HibernateUtil操作SessionFactory, Session and Transaction。
 * 包装Hibernate,提供基本的数据访问。
 */
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.ResultSetDynaClass;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.core.web.Paging;

public class PersistenceManager {
    private static Log log = LogFactory.getLog(PersistenceManager.class);
    private final String mstrNames[] = new String[0];
    private final Object mobjValues[] = new Object[0];

    public PersistenceManager() {
    }

    /**
     * 保存一个对象,调用saveObjs实现。
     */
    public void saveObj(Object obj) throws PersistenceException {
        List mobjValues = new ArrayList();
        mobjValues.add(obj);
        saveObjs(mobjValues);
    }

    /**
     * 保存数组中的所有对象。
     * 借助Hibernate的session.save(obj)方法实现。
     */
    public void saveObjs(List objs) throws PersistenceException {
        if (objs == null) {
            log.equals("保存失败,持久化对象为空!");
            throw new PersistenceException("保存失败,持久化对象为空!");
        }
        try {
            Session session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();

            for (int j = 0; j < objs.size(); j++) {
                log.info("objs.size() == " + objs.size());
                if (objs.get(j) == null) {
                    throw new PersistenceException("保存失败,持久化对象为空!");
                }
                session.save(objs.get(j));
                session.flush();
            }

            HibernateUtil.commitTransaction();
            HibernateUtil.closeSession();

        } catch (Exception ex) {
            log.error("saveObjs方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
    }

    public Object mergeObj(Object obj) throws PersistenceException {
        Object ret = null;
        try {
            Session session = HibernateUtil.getSession();
            ret = session.merge(obj);
        } catch (Exception ex) {
            log.error("mergeObj方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
        return ret;
    }

    /**
     * 修改一个对象,调用updateObjs实现。
     */
    public void updateObj(Object obj) throws PersistenceException {
        List mobjValues = new ArrayList();
        mobjValues.add(obj);
        updateObjs(mobjValues);
    }

    /**
     * 修改数组中的所有对象。
     * 借助Hibernate的session.update(obj)方法实现。
     */
    public void updateObjs(List objs) throws PersistenceException {
        if (objs == null) {
            log.equals("保存失败,持久化对象为空!");
            throw new PersistenceException("保存失败,持久化对象为空!");
        }
        try {
            Session session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();

            for (int j = 0; j < objs.size(); j++) {
                if (objs.get(j) == null) {
                    throw new PersistenceException("保存失败,持久化对象为空!");
                }
                Object updObj = session.merge(objs.get(j));
                session.update(updObj);
                session.flush();
            }

            HibernateUtil.commitTransaction();
            HibernateUtil.closeSession();

        } catch (Exception ex) {
            log.error("updateObjs方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
    }

    /**
     * 保存或更新一个对象,调用saveObjs实现。
     */
    public void saveOrUpdateObj(Object obj) throws PersistenceException {
        List mobjValues = new ArrayList();
        mobjValues.add(obj);
        saveOrUpdateObjs(mobjValues);
    }

    /**
     * 保存或更新数组中的所有对象。
     * 借助Hibernate的session.saveOrUpdate(obj)方法实现。
     */
    public void saveOrUpdateObjs(List objs) throws PersistenceException {
        if (objs == null) {
            log.equals("保存失败,持久化对象为空!");
            throw new PersistenceException("保存失败,持久化对象为空!");
        }
        try {
            Session session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();

            for (int j = 0; j < objs.size(); j++) {
                if (objs.get(j) == null) {
                    throw new PersistenceException("保存失败,持久化对象为空!");
                }
                session.saveOrUpdate(objs.get(j));
                session.flush();
            }

            HibernateUtil.commitTransaction();
            HibernateUtil.closeSession();

        } catch (Exception ex) {
            log.error("saveOrUpdateObjs方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
    }

    /**
     * 删除指定的对象!调用deleteObjs实现。
     */
    public void deleteObj(Object obj) throws PersistenceException { // ------  zyx  update 2005-12-08
        List mobjValues = new ArrayList();
        mobjValues.add(obj);
        deleteObjs(mobjValues);
    }

    /**
     * 删除数组中的所有对象。
     * 借助Hibernate的session.delete(obj)方法实现。
     */
    public void deleteObjs(List objs) throws PersistenceException {
        if (objs == null) {
            log.equals("保存失败,持久化对象为空!");
            throw new PersistenceException("保存失败,持久化对象为空!");
        }
        try {
            Session session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();
            for (int j = 0; j < objs.size(); j++) {
                if (objs.get(j) == null) {
                    throw new PersistenceException("保存失败,持久化对象为空!");
                }
                Object delObj = session.merge(objs.get(j));
                session.delete(delObj);
            }
            HibernateUtil.commitTransaction();
            HibernateUtil.closeSession();

        } catch (Exception ex) {
            log.error("deleteObjs方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
    }

    /**
     * 通过HQL语句删除指定的对象!
     * 借助Hibernate的session.delete(obj)方法实现。
     */
    public void deleteObjByHql(String hql) throws PersistenceException {
        try {
            Session session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();

            Query query = session.createQuery(hql);
            query.executeUpdate();

            HibernateUtil.commitTransaction();
            HibernateUtil.closeSession();

        } catch (Exception ex) {
            log.error("deleteObjByHql方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
    }

    /**
     * 通过对象的主键返回整个对象。
     * 借助Hibernate的session.load(class1, serializable)方法实现。
     */
    public Object loadObj(Class class1, Serializable serializable) throws PersistenceException {
        Object obj = null;
        try {
            Session session = HibernateUtil.getSession();
            obj = session.load(class1, serializable);
            HibernateUtil.closeSession();
        } catch (Exception ex) {
            log.error("loadObj方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
        return obj;
    }

    /**
     * 通过对象的主键返回整个对象,可以修改。
     * 借助Hibernate的session.load(class1, serializable, lockmode)方法实现。
     */
    public Object loadObjForUpdate(Class class1, Serializable serializable, LockMode lockmode) throws PersistenceException {
        Object obj = null;
        try {
            Session session = HibernateUtil.getSession();
            obj = session.load(class1, serializable, lockmode);
            HibernateUtil.closeSession();
        } catch (Exception ex) {
            log.error("loadObjForUpdate方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
        return obj;
    }

    /**
     * 通过查询语句返回整个对象。
     * 借助Hibernate的session.find(hql)方法实现。
     */
    public List queryObjs(String hql) throws PersistenceException {
        //log.info("======##==hql: " + hql);
        List list = null;
        try {
            Session session = HibernateUtil.getSession();
            list = session.createQuery(hql).list();
            HibernateUtil.closeSession();
        } catch (Exception ex) {
            log.error("queryObjs方法出错:" + ex.toString());
            HibernateUtil.closeSession();
            throw new PersistenceException(ex);
        }
        return list;
    }

    /**
     * 查询数据。
     */
    public List queryObjs(String hql, int i, int max) throws PersistenceException {
        return queryObjs(hql, mstrNames, mobjValues, i, max);
    }

    /**
     * 查询数据。
     * 借助Hibernate的query.list();方法实现。
     */
    public List queryObjs(String hql, String names[], Object values[], int first, int max) throws PersistenceException {
        //log.info("======##==hql: " + hql);
        List list = null;
        try {
            Session session = HibernateUtil.getSession();
            Query query = session.createQuery(hql);
            if (names != null) {
                for (int i = 0; i < names.length; i++) {

⌨️ 快捷键说明

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