欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

J2EE常用工具类—Hibernate操作

程序员文章站 2022-05-24 18:13:21
...
package cn.org.jshuwei.j2ee.util;

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
* 
* Hibernate操作的工具类
* 
* @author huwei(jshuwei.org.cn)
* @since 1.0
* 
*/
public class HbnUtil {
    private static SessionFactory sf;

    /**
     * 得到SessionFactory
     * 
     * @since 1.0
     * @return 返回SessionFactory
     */
    public static SessionFactory getSessionFactory() {
        if (sf == null || sf.isClosed()) {
            try {
                sf = new Configuration().configure().buildSessionFactory();
            } catch (HibernateException e) {
                e.printStackTrace();
            }
        }
        return sf;
    }

    /**
     * 得到Session
     * 
     * @since 1.0
     * @return 返回Session
     */
    public static Session getSession() {
        getSessionFactory();
        return sf.openSession();
    }

    /**
     * 释放SessionFactory
     * 
     * @since 1.0
     */
    public static void releaseSessionFactory() {
        if (sf != null && !sf.isClosed()) {
            sf.close();
        }
    }

    /**
     * 保存对象
     * 
     * @since 1.0
     * @param obj
     *            需要保存的对象
     */
    public static Serializable add(Object obj) {
        Session session = null;
        Serializable ret = null;
        try {
            session = getSession();
            session.beginTransaction();
            ret = session.save(obj);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            if (session != null) {
                session.close();
            }
            releaseSessionFactory();
        }
        return ret;
    }

    /**
     * 删除对象
     * 
     * @since 1.0
     * @param obj
     *            需要删除的对象
     */
    public static void delete(Object obj) {
        Session session = null;
        try {
            session = getSession();
            session.beginTransaction();
            session.delete(obj);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            if (session != null) {
                session.close();
            }
            releaseSessionFactory();
        }
    }

    /**
     * 更新对象
     * 
     * @since 1.0
     * @param obj
     *            需要更新的对象
     */
    public static void update(Object obj) {
        Session session = null;
        try {
            session = getSession();
            session.beginTransaction();
            session.update(obj);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            if (session != null) {
                session.close();
            }
            releaseSessionFactory();
        }
    }

    /**
     * 查找对象
     * 
     * @since 1.0
     * @param clazz 需要查找的对象类型
     * @param id
     *            需要查找的对象的id
     */
    public static Object find(Class clazz,Serializable id) {
        Session session = null;
        Object obj=null;
        try {
            session = getSession();
            obj=session.get(clazz, id);
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
            releaseSessionFactory();
        }
        return obj;
    }
}

 

相关标签: Hibernate