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

📄 basehbmdao.java

📁 网上选课系统 供学习用 初学者可以看看 各种功能还算完善吧
💻 JAVA
字号:
package com.student.dao;

import net.sf.hibernate.*;
public class BaseHbmDAO
{

    public BaseHbmDAO()
    {
        autoClose = true;
        isTransaction = false;
    }

    public BaseHbmDAO(Session aSes)
    {
        autoClose = true;
        isTransaction = false;
        ses = aSes;
    }

    public void setSession(Session aSes)
    {
        ses = aSes;
    }

    public void remove(Object obj)
        throws DAOException
    {
        Transaction trans = null;
        try
        {
            trans = ses.beginTransaction();
            ses.delete(obj);
            trans.commit();
        }
        catch(Exception e)
        {
            try
            {
                if(trans != null)
                    trans.rollback();
            }
            catch(Exception ex)
            {
                e.printStackTrace();
            }
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
    }

    public void remove(Class clazz, String id)
        throws DAOException
    {
        Transaction trans = null;
        try
        {
            trans = ses.beginTransaction();
            Object obj = ses.load(clazz, id);
            ses.delete(obj);
            trans.commit();
        }
        catch(Exception e)
        {
            try
            {
                if(trans != null)
                    trans.rollback();
            }
            catch(Exception ex)
            {
                e.printStackTrace();
            }
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
    }

    public void remove(Class clazz, Long id)
        throws DAOException
    {
        Transaction trans = null;
        try
        {
            trans = ses.beginTransaction();
            Object obj = ses.load(clazz, id);
            ses.delete(obj);
            trans.commit();
        }
        catch(Exception e)
        {
            try
            {
                if(trans != null)
                    trans.rollback();
            }
            catch(Exception ex)
            {
                e.printStackTrace();
            }
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
    }

    public void remove(String hql)
        throws DAOException
    {
        Transaction trans = null;
        try
        {
            trans = ses.beginTransaction();
            ses.delete(hql);
            trans.commit();
        }
        catch(Exception e)
        {
            try
            {
                if(trans != null)
                    trans.rollback();
            }
            catch(Exception ex)
            {
                e.printStackTrace();
            }
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
    }

    public Object retrieve(Class clazz, String id)
        throws DAOException
    {
        Object obj = null;
        try
        {
            obj = ses.load(clazz, id);
        }
        catch(Exception e)
        {
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
        return obj;
    }

    public Object retrieve(Class clazz, Long id)
        throws DAOException
    {
        Object obj = null;
        try
        {
            obj = ses.load(clazz, id);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
        return obj;
    }

    public void store(Object obj)
        throws DAOException
    {
        Transaction trans = null;
        try
        {
            trans = ses.beginTransaction();
            ses.saveOrUpdate(obj);
            trans.commit();
        }
        catch(Exception e)
        {
            try
            {
                if(trans != null)
                    trans.rollback();
            }
            catch(Exception ex)
            {
                e.printStackTrace();
            }
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
    }

    public void save(Object obj)
        throws DAOException
    {
        Transaction trans = null;
        try
        {
            trans = ses.beginTransaction();
            ses.save(obj);
            trans.commit();
        }
        catch(Exception e)
        {
            try
            {
                if(trans != null)
                    trans.rollback();
            }
            catch(Exception ex)
            {
                e.printStackTrace();
            }
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
    }

    public void update(Object obj)
        throws DAOException
    {
        Transaction trans = null;
        try
        {
            trans = ses.beginTransaction();
            ses.update(obj);
            ses.flush();
            trans.commit();
        }
        catch(Exception e)
        {
            try
            {
                if(trans != null)
                    trans.rollback();
            }
            catch(Exception ex)
            {
                e.printStackTrace();
            }
            throw new DAOException(e);
        }
        finally
        {
            if(autoClose)
                closeSession();
        }
    }

    public void closeSession()
    {
        try
        {
            ses.close();
        }
        catch(HibernateException ex)
        {
            ex.printStackTrace();
        }
    }

    public boolean isAutoClose()
    {
        return autoClose;
    }

    public void setAutoClose(boolean autoClose)
    {
        this.autoClose = autoClose;
    }

    protected Session ses;
    private boolean autoClose;
    private boolean isTransaction;
}

⌨️ 快捷键说明

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