ITeye博客

HibernateUtil 的写法

shizhi · 2008-12-24 12:08 · Java · Hibernate · Markdown


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (HibernateException ex) {
            throw new RuntimeException("Exception building SessionFactory: "
                    + ex.getMessage(), ex);
        }
    }
    public static Session getSession() throws HibernateException {
        Session s = sessionFactory.openSession();
        return s;
    }

    public static void closeSession(Session s) {
        s.close();
    }
}

评论(1)

  1. #1 楼 · wxm3373477 · 2009-03-06 09:48

    学习学习 :lol:

评论功能维护中,当前仅展示历史评论。