`
BBjava
  • 浏览: 120373 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

HibernateUtil.java

阅读更多
官方很简单的HibernateUtil.java,可以针对自己的需求改进。
package org.bbjava.funny.test;

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

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure("org/bbjava/funny/test/cfg/hibernate.cfg.xml").buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

对了,工具类大多都是静态的,为什么呢,留下这个问题来思考。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics